<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="MySQL相关知识点, 博客 blog">
    <meta name="description" content="熊猫小二的博客  xmxe&#39;s blog">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- 为了引用qq空间图床文件 -->
    <meta name="referrer" content="no-referrer">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>MySQL相关知识点 | 熊猫小二</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/blog/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/loading.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>



   
<style>
    body{
       background-image: url(/blog/medias/cover.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    
  <div id="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">加载中...</div>
    </div>
  </div>
  <!-- 页面加载动画 -->
  <script>
    $(document).ready(function () {
      // document.body.style.overflow = 'auto';
      document.getElementById('loading-box').classList.add("loaded")
    })
  </script>

    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                        <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">熊猫小二</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-book" style="zoom: 0.6;"></i>
      
      <span>归档</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/about">
          
          <i class="fas fa-star-of-david" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>主页</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/gallery">
          
          <i class="fas fa-images" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>相册</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  
    <li>
      <a class="waves-effect waves-light" onclick="switchNightMode()">
        <i id="sum-moon-icon" class="fas fa-sun" style="zoom:0.65;" title="切换主题"></i>
      </a>
    </li>
  
  
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
          <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">熊猫小二</div>
        <div class="logo-desc">
            
            熊猫小二的博客  xmxe&#39;s blog
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book"></i>
			
			归档
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-star-of-david" style="position: absolute;left:50px" ></i>
			      
		          <span>主页</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/gallery " style="margin-left:75px">
				  
				   <i class="fa fas fa-images" style="position: absolute;left:50px" ></i>
			      
		          <span>相册</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/10.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 id="post-title" class="description center-align post-title"></h1>

                    
                        <!-- <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script> -->
                        <script>
                            var typedObj = new Typed("#post-title", {
                                strings: [ 'MySQL相关知识点' ],
                                startDelay: 300,
                                typeSpeed: 70,
                                loop: false,
                                backSpeed: 50,
                                showCursor: true
                            });
                        </script>
                    
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
        background-color: rgb(255, 255, 255,0.7);
        border-radius: 10px;
        box-shadow: 0 10px 35px 2px rgba(0, 0, 0, .15), 0 5px 15px rgba(0, 0, 0, .07), 0 2px 5px -5px rgba(0, 0, 0, .1) !important;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
        max-height: 480px;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/blog/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="post-category">
                                数据库
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="MySQL应用知识点"><a href="#MySQL应用知识点" class="headerlink" title="MySQL应用知识点"></a>MySQL应用知识点</h2><h3 id="递归function-父子查询"><a href="#递归function-父子查询" class="headerlink" title="递归function,父子查询"></a>递归function,父子查询</h3><p>创建函数时注意分隔符，mysql遇到分号就执行，在创建函数的时候容易报错（use near ‘ ‘at line）,可使用<strong>delimiter &#x2F;&#x2F;</strong> 定义分隔符</p>
<p>delimiter &#x2F;&#x2F; (创建函数报错时添加这个)</p>
<p><strong>getParentList</strong></p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">FUNCTION</span> <span class="token identifier"><span class="token punctuation">`</span>getParentList<span class="token punctuation">`</span></span><span class="token punctuation">(</span>rootId <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">RETURNS</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
<span class="token keyword">DECLARE</span> fid <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">;</span>
<span class="token keyword">DECLARE</span> str <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span> <span class="token keyword">default</span> rootId<span class="token punctuation">;</span>
<span class="token keyword">WHILE</span> rootId <span class="token operator">is</span> <span class="token operator">not</span> <span class="token boolean">null</span> <span class="token keyword">do</span>
  <span class="token keyword">SET</span> fid <span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">SELECT</span> parentid <span class="token keyword">FROM</span> treeNodes <span class="token keyword">WHERE</span> id <span class="token operator">=</span> rootId<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">IF</span> fid <span class="token operator">is</span> <span class="token operator">not</span> <span class="token boolean">null</span> <span class="token keyword">THEN</span>
	<span class="token keyword">SET</span> str <span class="token operator">=</span> concat<span class="token punctuation">(</span>str<span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">,</span> fid<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">SET</span> rootId <span class="token operator">=</span> fid<span class="token punctuation">;</span>
  <span class="token keyword">ELSE</span>
	<span class="token keyword">SET</span> rootId <span class="token operator">=</span> fid<span class="token punctuation">;</span>
  <span class="token keyword">END</span> <span class="token keyword">IF</span><span class="token punctuation">;</span>
<span class="token keyword">END</span> <span class="token keyword">WHILE</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> str<span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span><span class="token comment">-- (这样只有当//出现之后，MySQL解释器才会执行这段语句)</span>

<span class="token keyword">delimiter</span> <span class="token punctuation">;</span> <span class="token comment">-- (改回默认的MySQL delimiter：“;”）</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>使用：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> getParentList<span class="token punctuation">(</span><span class="token string">'001001'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tbl <span class="token keyword">where</span> FIND_IN_SET<span class="token punctuation">(</span>id<span class="token punctuation">,</span>getParentList<span class="token punctuation">(</span><span class="token string">'001001'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>getChildList</strong></p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 在root用户下创建 %代表任何ip都可以连接</span>
<span class="token keyword">CREATE</span> <span class="token keyword">DEFINER</span><span class="token operator">=</span><span class="token identifier"><span class="token punctuation">`</span>root<span class="token punctuation">`</span></span><span class="token variable">@`%`</span> <span class="token keyword">FUNCTION</span> <span class="token identifier"><span class="token punctuation">`</span>getChildList<span class="token punctuation">`</span></span><span class="token punctuation">(</span>rootId <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">RETURNS</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span> <span class="token keyword">CHARSET</span> utf8
<span class="token keyword">BEGIN</span>
<span class="token keyword">DECLARE</span> str <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">DECLARE</span> cid <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 注意设置的长度,过短的话当树很高的时候容易导致查询数据不全</span>
<span class="token keyword">SET</span> str <span class="token operator">=</span> <span class="token string">'$'</span><span class="token punctuation">;</span>
<span class="token keyword">SET</span> cid <span class="token operator">=</span> rootId<span class="token punctuation">;</span>
<span class="token keyword">WHILE</span> cid <span class="token operator">is</span> <span class="token operator">not</span> <span class="token boolean">null</span> <span class="token keyword">DO</span>
  <span class="token keyword">SET</span> str <span class="token operator">=</span> concat<span class="token punctuation">(</span>str<span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">,</span> cid<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">SELECT</span> group_concat<span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token keyword">INTO</span> cid <span class="token keyword">FROM</span> fke_supervision_template <span class="token keyword">where</span> FIND_IN_SET<span class="token punctuation">(</span>parent_id<span class="token punctuation">,</span> cid<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">END</span> <span class="token keyword">WHILE</span><span class="token punctuation">;</span>
 <span class="token keyword">RETURN</span> str<span class="token punctuation">;</span>
<span class="token keyword">END</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>使用：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> getChildList<span class="token punctuation">(</span><span class="token string">'001001001'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tbl <span class="token keyword">where</span> FIND_IN_SET<span class="token punctuation">(</span>id<span class="token punctuation">,</span>getChildList<span class="token punctuation">(</span><span class="token string">'001001001'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>FIND_IN_SET(str,strlist)</strong>,str-要查询的字符串,strlist-字段名参数以”,”分隔如(1,2,6,8)。查询字段(strlist)中包含(str)的结果，返回结果为null或结果。<code>SELECT FIND_IN_SET(&#39;b&#39;,&#39;a,b,c,d&#39;);</code>因为b在strlist集合中放在2的位置,下标从1开始</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> treenodes <span class="token keyword">where</span> FIND_IN_SET<span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token string">'1,2,3,4,5'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>使用find_in_set函数一次返回多条记录，id是一个表的字段，然后每条记录分别是id等于1，2，3，4，5的时候，有点类似in（集合）</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> treenodes <span class="token keyword">where</span> id <span class="token operator">in</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><strong>当使用find_in_set配合递归函数完成递归查询时，有时会查询很慢，此时sql可以这样优化</strong></p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> <span class="token punctuation">(</span><span class="token keyword">select</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token keyword">from</span> t1 <span class="token keyword">join</span> t2 <span class="token keyword">on</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token keyword">temp</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token keyword">select</span> getChildList<span class="token punctuation">(</span><span class="token string">'10001'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> cids<span class="token punctuation">)</span> <span class="token keyword">where</span> find_in_set<span class="token punctuation">(</span>id<span class="token punctuation">,</span>cids<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/wokelv/article/details/78915502">MySQL中FIND_IN_SET函数执行非常慢的某种写法</a></p>
</blockquote>
<p><strong>MySQL8.0使用CTE完成递归查询</strong></p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">WITH</span> RECURSIVE cte <span class="token keyword">AS</span> <span class="token punctuation">(</span>
<span class="token keyword">SELECT</span>  ID<span class="token punctuation">,</span>  PID<span class="token punctuation">,</span> NAME<span class="token punctuation">,</span> <span class="token keyword">LEVEL</span><span class="token punctuation">,</span> <span class="token keyword">Type</span>  <span class="token keyword">FROM</span> tmp_zjs  <span class="token keyword">WHERE</span>  ID <span class="token operator">=</span> <span class="token string">'102'</span> <span class="token keyword">UNION</span> <span class="token keyword">ALL</span> 
<span class="token keyword">SELECT</span> sou<span class="token punctuation">.</span>ID<span class="token punctuation">,</span> sou<span class="token punctuation">.</span>PID<span class="token punctuation">,</span> sou<span class="token punctuation">.</span>NAME<span class="token punctuation">,</span> sou<span class="token punctuation">.</span><span class="token keyword">LEVEL</span><span class="token punctuation">,</span> sou<span class="token punctuation">.</span><span class="token keyword">Type</span>  <span class="token keyword">FROM</span> 
cte c <span class="token keyword">INNER</span> <span class="token keyword">JOIN</span> tmp_zjs sou <span class="token keyword">ON</span> c<span class="token punctuation">.</span>ID <span class="token operator">=</span> sou<span class="token punctuation">.</span>PID  <span class="token punctuation">)</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> cte<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.begtut.com/mysql/mysql-recursive-cte.html">MySQL使用递归CTE遍历分层数据</a><br><a target="_blank" rel="noopener" href="https://www.yiibai.com/mysql/cte.html">MySQL CTE(公共表表达式)</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ymAjaaKKwgbpkHOZ9tgbJw">一种避免递归查询所有子部门的树数据表设计与实现</a></p>
</blockquote>
<h3 id="触发器"><a href="#触发器" class="headerlink" title="触发器"></a>触发器</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TRIGGER</span> trigger_name
trigger_time
trigger_event <span class="token keyword">ON</span> tbl_name
<span class="token keyword">FOR EACH ROW</span>
trigger_stmt
<span class="token comment">-- 其中：</span>
<span class="token comment">-- trigger_name：标识触发器名称，用户自行指定；</span>
<span class="token comment">--trigger_time：标识触发时机，取值为BEFORE或AFTER；</span>
<span class="token comment">-- trigger_event：标识触发事件，取值为INSERT、UPDATE或DELETE；</span>
<span class="token comment">-- tbl_name：标识建立触发器的表名，即在哪张表上建立触发器；</span>
<span class="token comment">-- trigger_stmt：触发器程序体，可以是一句SQL语句，或者用BEGIN和END包含的多条语句。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="存储过程-游标"><a href="#存储过程-游标" class="headerlink" title="存储过程(游标)"></a>存储过程(游标)</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">DEFINER</span><span class="token operator">=</span><span class="token identifier"><span class="token punctuation">`</span>root<span class="token punctuation">`</span></span><span class="token variable">@`%`</span> <span class="token keyword">PROCEDURE</span> <span class="token identifier"><span class="token punctuation">`</span>NewProc<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token operator">IN</span> a <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">OUT</span> b <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
<span class="token keyword">declare</span> my_id <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">-- 自定义变量1</span>
<span class="token keyword">declare</span> my_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">-- 自定义变量2</span>
<span class="token keyword">DECLARE</span> done <span class="token keyword">INT</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">FALSE</span><span class="token punctuation">;</span> <span class="token comment">-- 自定义控制游标循环变量,默认false</span>
<span class="token keyword">DECLARE</span> My_Cursor <span class="token keyword">CURSOR</span> <span class="token keyword">FOR</span> <span class="token punctuation">(</span> <span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> area_name <span class="token keyword">FROM</span> fke_area <span class="token keyword">where</span> id <span class="token operator">&lt;</span> <span class="token number">20</span> <span class="token keyword">order</span> <span class="token keyword">by</span> id <span class="token keyword">desc</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">-- 定义游标并输入结果集</span>
<span class="token keyword">DECLARE</span> <span class="token keyword">CONTINUE</span> <span class="token keyword">HANDLER</span> <span class="token keyword">FOR</span> <span class="token operator">NOT</span> FOUND <span class="token keyword">SET</span> done <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">;</span> <span class="token comment">-- 绑定控制变量到游标,游标循环结束自动转true</span>

<span class="token keyword">OPEN</span> My_Cursor<span class="token punctuation">;</span> <span class="token comment">-- 打开游标</span>

myLoop: <span class="token keyword">LOOP</span> <span class="token comment">-- 开始循环体,myLoop为自定义循环名,结束循环时用到</span>
	<span class="token keyword">FETCH</span> My_Cursor <span class="token keyword">into</span> my_id<span class="token punctuation">,</span> my_name<span class="token punctuation">;</span> <span class="token comment">-- 将游标当前读取行的数据顺序赋予自定义变量</span>
	<span class="token keyword">IF</span> done <span class="token keyword">THEN</span> <span class="token comment">-- 判断是否继续循环</span>
		<span class="token keyword">LEAVE</span> myLoop<span class="token punctuation">;</span> <span class="token comment">-- 结束循环 (break) iterate myLoop(continue)</span>
	<span class="token keyword">END</span> <span class="token keyword">IF</span><span class="token punctuation">;</span>
  <span class="token comment">-- 自己要做的事情,在 sql 中直接使用自定义变量即可</span>
	<span class="token keyword">IF</span> <span class="token punctuation">(</span>my_id <span class="token operator">&lt;</span> <span class="token number">7</span> <span class="token punctuation">)</span> <span class="token keyword">THEN</span>
		<span class="token keyword">set</span> b <span class="token operator">=</span> my_name<span class="token punctuation">;</span>
	<span class="token keyword">ELSEIF</span><span class="token punctuation">(</span>a<span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token keyword">THEN</span> <span class="token comment">-- 不用"=="做判断,用'='</span>
		<span class="token keyword">set</span> b <span class="token operator">=</span> <span class="token string">'q'</span><span class="token punctuation">;</span>
	<span class="token keyword">ELSE</span>
		<span class="token keyword">set</span> b <span class="token operator">=</span> <span class="token string">'qq'</span><span class="token punctuation">;</span>
	<span class="token keyword">END</span> <span class="token keyword">IF</span><span class="token punctuation">;</span>
  <span class="token comment">-- COMMIT; -- 提交事务</span>
<span class="token keyword">END</span> <span class="token keyword">LOOP</span> myLoop<span class="token punctuation">;</span> <span class="token comment">-- 结束自定义循环体</span>
<span class="token keyword">CLOSE</span> My_Cursor<span class="token punctuation">;</span> <span class="token comment">-- 关闭游标</span>
<span class="token keyword">END</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>调用</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">set</span> <span class="token variable">@a</span> <span class="token operator">=</span> <span class="token string">'a'</span><span class="token punctuation">;</span><span class="token keyword">set</span> <span class="token variable">@b</span> <span class="token operator">=</span> <span class="token string">'b'</span><span class="token punctuation">;</span><span class="token keyword">call</span> NewProc<span class="token punctuation">(</span><span class="token variable">@a</span><span class="token punctuation">,</span><span class="token variable">@b</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">select</span> <span class="token variable">@b</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>demo</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">DEFINER</span><span class="token operator">=</span><span class="token identifier"><span class="token punctuation">`</span>root<span class="token punctuation">`</span></span><span class="token variable">@`%`</span> <span class="token keyword">PROCEDURE</span> <span class="token identifier"><span class="token punctuation">`</span>zxjc_city_month_data<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token operator">IN</span> <span class="token identifier"><span class="token punctuation">`</span>table_name<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">IN</span> <span class="token identifier"><span class="token punctuation">`</span>start_time<span class="token punctuation">`</span></span> <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">IN</span> <span class="token identifier"><span class="token punctuation">`</span>end_time<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
	<span class="token keyword">DECLARE</span> ydfh_table <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">DECLARE</span> fdcl_table <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">DECLARE</span> dates <span class="token keyword">date</span><span class="token punctuation">;</span>
	<span class="token keyword">DECLARE</span> datee <span class="token keyword">date</span><span class="token punctuation">;</span>
	<span class="token keyword">delete</span> <span class="token keyword">from</span> zxjc_city_month_data <span class="token keyword">where</span> <span class="token keyword">time</span> <span class="token operator">=</span> table_name<span class="token punctuation">;</span>
	<span class="token keyword">set</span> dates <span class="token operator">=</span> DATE_FORMAT<span class="token punctuation">(</span>start_time<span class="token punctuation">,</span><span class="token string">'%Y-%m-%d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">set</span> datee <span class="token operator">=</span> DATE_FORMAT<span class="token punctuation">(</span>end_time<span class="token punctuation">,</span><span class="token string">'%Y-%m-%d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">set</span> ydfh_table <span class="token operator">=</span> concat<span class="token punctuation">(</span><span class="token string">'zxjc_dp_sspwrgrid_his_'</span><span class="token punctuation">,</span>table_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">set</span> fdcl_table <span class="token operator">=</span> concat<span class="token punctuation">(</span><span class="token string">'zxjc_dp_ssgenerator_his_'</span><span class="token punctuation">,</span>table_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">set</span> <span class="token variable">@str</span> <span class="token operator">=</span> concat<span class="token punctuation">(</span><span class="token string">'insert into zxjc_city_month_data(ydfh,fdcl,grl,ydl,rdb,city,time) SELECT round(ydfh.ydfh,2) as ydfh,round(fdcl.fdcl,2) as fdcl,round(grl.grl,2) as grl,round(( ydfh.ydfh * 1000 )/( 24 * 30 ),2) as ydl,round( grl.grl / ( grl.fdl * 3600 )* 100, 2 ) as rdb,dm.dq as city,\''</span><span class="token punctuation">,</span>table_name <span class="token punctuation">,</span> <span class="token string">'\' as time	FROM bi_xzqh_dm dm LEFT JOIN ( SELECT sum( VALUE ) ydfh, id FROM '</span> <span class="token punctuation">,</span> ydfh_table <span class="token punctuation">,</span> <span class="token string">' WHERE meas_type = \'93872001\' AND create_time BETWEEN \''</span><span class="token punctuation">,</span>start_time<span class="token punctuation">,</span><span class="token string">'\' AND \''</span><span class="token punctuation">,</span>end_time<span class="token punctuation">,</span><span class="token string">'\' GROUP BY id ) ydfh ON ydfh.id = concat( \'0101\', dm.xzqh_dm ) LEFT JOIN ( SELECT city, sum( VALUE ) fdcl FROM '</span> <span class="token punctuation">,</span> fdcl_table <span class="token punctuation">,</span> <span class="token string">' WHERE create_time BETWEEN \''</span> <span class="token punctuation">,</span>start_time<span class="token punctuation">,</span> <span class="token string">'\' AND \''</span> <span class="token punctuation">,</span>end_time<span class="token punctuation">,</span><span class="token string">'\' GROUP BY city ) fdcl ON fdcl.city = dm.dq LEFT JOIN ( SELECT city,round(ifnull(sum(
CASE WHEN (oms_rfdl=0 || oms_rfdl is null) and tmr_rfdl!=0 and tmr_rfdl is not null THEN tmr_rfdl
     WHEN (tmr_rfdl = 0||tmr_rfdl is NULL) and dfdc_rfdl!=0 and dfdc_rfdl is not null THEN dfdc_rfdl
     WHEN (dfdc_rfdl = 0||dfdc_rfdl is NULL) and qsh_rfdl!=0 and qsh_rfdl is not null THEN qsh_rfdl
     WHEN (qsh_rfdl = 0||qsh_rfdl is NULL) and gl_rfdl!=0 and gl_rfdl is not null THEN gl_rfdl
ELSE oms_rfdl END ),0),2) as fdl, ROUND(IFNULL(sum(grl),0),2) as grl FROM zxjc_dp_fdjdata_day WHERE date_time BETWEEN \''</span><span class="token punctuation">,</span>dates<span class="token punctuation">,</span><span class="token string">'\' AND \''</span><span class="token punctuation">,</span>datee<span class="token punctuation">,</span><span class="token string">'\' GROUP BY city ) grl ON grl.city = dm.dq'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">-- 			select @str;</span>
	<span class="token keyword">PREPARE</span> cm <span class="token keyword">FROM</span> <span class="token variable">@str</span><span class="token punctuation">;</span>
	<span class="token keyword">EXECUTE</span> cm<span class="token punctuation">;</span>
<span class="token keyword">END</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>调用</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">call</span> zxjc_city_month_data<span class="token punctuation">(</span><span class="token string">'202308'</span><span class="token punctuation">,</span> <span class="token string">'2023-08-01 00:00:00'</span><span class="token punctuation">,</span> <span class="token string">'2023-08-31 23:59:59'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>如果调用输出out的话</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">FUNCTION</span> myfunc<span class="token punctuation">(</span><span class="token keyword">OUT</span> out_param1 <span class="token keyword">INT</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">OUT</span> out_param2 <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
<span class="token keyword">SET</span> out_param1<span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">SET</span> out_param2<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span>
<span class="token keyword">CALL</span> myfunc<span class="token punctuation">(</span><span class="token variable">@a</span><span class="token punctuation">,</span><span class="token variable">@b</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> CONCAT<span class="token punctuation">(</span><span class="token string">'out_param1='</span><span class="token punctuation">,</span><span class="token variable">@a</span><span class="token punctuation">,</span><span class="token string">' out_param2='</span><span class="token punctuation">,</span><span class="token variable">@b</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">-- 或者</span>
<span class="token keyword">CREATE</span> <span class="token keyword">PROCEDURE</span> <span class="token identifier"><span class="token punctuation">`</span>get_total_orders<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token keyword">OUT</span> total <span class="token keyword">INT</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
	<span class="token keyword">SELECT</span> COUNT <span class="token keyword">into</span> total <span class="token keyword">from</span> orders<span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token punctuation">;</span>
<span class="token comment">-- set @total = 1;</span>
<span class="token keyword">CALL</span> get_total_orders<span class="token punctuation">(</span><span class="token variable">@total</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> <span class="token variable">@total</span> <span class="token keyword">as</span> total<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="存储过程和函数的区别"><a href="#存储过程和函数的区别" class="headerlink" title="存储过程和函数的区别"></a>存储过程和函数的区别</h4><p>一、含义不同</p>
<p>1、存储过程：存储过程是SQL 语句和可选控制流语句的预编译集合，以一个名称存储并作为一个单元处理。<br>2、函数：是由一个或多个 SQL 语句组成的子程序，可用于封装代码以便重新使用。 函数限制比较多，如不能用临时表，只能用表变量等</p>
<p>二、使用条件不同</p>
<p>1、存储过程：可以在单个存储过程中执行一系列 SQL 语句。而且可以从自己的存储过程内引用其它存储过程，这可以简化一系列复杂语句。<br>2、函数：自定义函数诸多限制，有许多语句不能使用，许多功能不能实现。函数可以直接引用返回值，用表变量返回记录集。但是，用户定义函数不能用于执行一组修改全局数据库状态的操作。</p>
<p>三、执行方式不同</p>
<p>1、存储过程：存储过程可以返回参数，如记录集，函数只能返回值或者表对象。存储过程的参数有in，out，inout三种，存储过程声明时不需要返回类型。<br>2、函数：函数参数只有in，而函数需要描述返回类型，且函数中必须包含一个有效的return语句。</p>
<h3 id="MySQL事件-如定时执行存储过程"><a href="#MySQL事件-如定时执行存储过程" class="headerlink" title="MySQL事件(如定时执行存储过程)"></a>MySQL事件(如定时执行存储过程)</h3><ol>
<li>确保事件调度器已启用。您可以通过运行以下命令来检查和启用事件调度器（如果未启用）：</li>
</ol>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 查看启动器状态 ON为打开 OFF为关闭</span>
<span class="token keyword">SHOW</span> VARIABLES <span class="token operator">LIKE</span> <span class="token string">'event_scheduler'</span><span class="token punctuation">;</span>
<span class="token comment">-- 打开</span>
<span class="token keyword">SET</span> <span class="token keyword">GLOBAL</span> EVENT_SCHEDULER <span class="token operator">=</span> <span class="token keyword">ON</span><span class="token punctuation">;</span>
<span class="token comment">-- 或者my.ini配置文件下加入</span>
<span class="token punctuation">[</span>mysqld<span class="token punctuation">]</span>
event_scheduler <span class="token operator">=</span> <span class="token keyword">ON</span>
<span class="token comment">-- 然后重启MySQL</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li><p>创建一个存储过程</p>
</li>
<li><p>创建事件</p>
</li>
</ol>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> EVENT event_name <span class="token keyword">ON</span> SCHEDULE
<span class="token comment">-- AT '2023-07-19 10:00:00' -- 某一时间执行一次</span>
<span class="token comment">-- AT CURRENT_TIMESTAMP</span>
EVERY <span class="token number">1</span> <span class="token keyword">DAY</span> <span class="token comment">-- 设置执行间隔，例如每天</span>
STARTS <span class="token string">'2023-07-19 10:00:00'</span> <span class="token comment">-- 设置事件的开始时间</span>
ENDS <span class="token string">'2023-07-21 10:00:00'</span> <span class="token comment">-- 设置事件的结束时间（可选）</span>
<span class="token keyword">ON</span> COMPLETION PRESERVE <span class="token comment">-- 使事件在完成后保持有效（可选）</span>
<span class="token keyword">ENABLE</span> <span class="token comment">-- 启用事件（必需）</span>
<span class="token keyword">COMMENT</span> <span class="token string">'这是一个定时执行存储过程的事件'</span>
<span class="token keyword">DO</span>
    <span class="token keyword">CALL</span> my_stored_procedure<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">-- 调用存储过程</span>
<span class="token comment">-- DO CALL procedure_name('input1', @output1);</span>

<span class="token comment">-- 选项</span>
<span class="token keyword">CREATE</span>
	<span class="token punctuation">[</span><span class="token keyword">DEFINER</span> <span class="token operator">=</span> &#123; <span class="token keyword">user</span> <span class="token operator">|</span> <span class="token keyword">CURRENT_USER</span> &#125;<span class="token punctuation">]</span>
    EVENT
    <span class="token punctuation">[</span><span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span><span class="token punctuation">]</span>
    event_name
    <span class="token keyword">ON</span> SCHEDULE schedule
    <span class="token punctuation">[</span><span class="token keyword">ON</span> COMPLETION <span class="token punctuation">[</span><span class="token operator">NOT</span><span class="token punctuation">]</span> PRESERVE<span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token keyword">ENABLE</span> <span class="token operator">|</span> <span class="token keyword">DISABLE</span> <span class="token operator">|</span> <span class="token keyword">DISABLE</span> <span class="token keyword">ON</span> SLAVE<span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token keyword">COMMENT</span> <span class="token string">'comment'</span><span class="token punctuation">]</span>
    <span class="token keyword">DO</span> event_body<span class="token punctuation">;</span> 
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<table>
<thead>
<tr>
<th align="center">sql语法</th>
<th align="center"><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td align="center">definer</td>
<td align="center"><strong>可选项</strong>，给指定用户使用权限</td>
</tr>
<tr>
<td align="center">if not exists</td>
<td align="center"><strong>可选项</strong>，用于判断要创建的事件是否存在</td>
</tr>
<tr>
<td align="center">event event_name</td>
<td align="center"><strong>必选项</strong>，指定事件名称，event_name的最大长度为64个字符，如果为指定event_name，则默认为当前的mysql用户名（不区分大小写）</td>
</tr>
<tr>
<td align="center">on schedule schedule</td>
<td align="center"><strong>必选项</strong>，这里的schedule用于定义执行的时间和时间间隔，在下面我们详细讲解</td>
</tr>
<tr>
<td align="center">on completion [not] preserve</td>
<td align="center"><strong>可选项</strong>，配置事件执行完一次后的处理方式；当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当event到期的时候,该event会被自动删除掉.</td>
</tr>
<tr>
<td align="center">enable、disable、disable on slave</td>
<td align="center"><strong>可选项</strong>，用于指定事件的一种属性。enable表示该事件是开启的，也就是调度器检查事件是否必选调用；disable表示该事件是关闭的，也就是事件的声明存储到目录中，但是调度器不会检查它是否应该调用；disable on slave表示事件在从机中是关闭的。如果不指定这三个选择中的任意一个，则在一个事件创建之后，它立即变为活动的。</td>
</tr>
<tr>
<td align="center">comment ‘comment’</td>
<td align="center"><strong>可选项</strong>，用于定义事件的注释</td>
</tr>
<tr>
<td align="center">do event_body</td>
<td align="center"><strong>必选项</strong>，用于指定事件启动时所要执行的代码。可以是任何有效的sql语句、存储过程或者一个计划执行的事件。如果包含多条语句，可以使用begin…end复合结构</td>
</tr>
</tbody></table>
<ol start="4">
<li>查看事件</li>
</ol>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">show</span> events<span class="token punctuation">;</span>
<span class="token comment">-- 条件筛选</span>
<span class="token keyword">show</span> events <span class="token operator">like</span> <span class="token string">'%event_name%'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<ol start="5">
<li>修改事件</li>
</ol>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">--开启事件</span>
<span class="token keyword">alter</span> event event_name <span class="token keyword">enable</span><span class="token punctuation">;</span>
<span class="token comment">--关闭事件</span>
<span class="token keyword">alter</span> event event_name <span class="token keyword">disable</span><span class="token punctuation">;</span>
<span class="token comment">-- 修改执行计划</span>
<span class="token keyword">ALTER</span> EVENT event_name <span class="token keyword">ON</span> SCHEDULE schedule<span class="token punctuation">;</span>
<span class="token comment">-- 修改事件主体</span>
<span class="token keyword">ALTER</span> EVENT event_name <span class="token keyword">DO</span> event_body<span class="token punctuation">;</span>
<span class="token comment">-- 重命名事件</span>
<span class="token keyword">ALTER</span> EVENT event_name <span class="token keyword">RENAME</span> <span class="token keyword">TO</span> new_event_name<span class="token punctuation">;</span>
<span class="token comment">-- 移动事件到其他数据库</span>
<span class="token comment">-- 可修改事件名称</span>
<span class="token keyword">ALTER</span> EVENT demo<span class="token punctuation">.</span>event_name <span class="token keyword">RENAME</span> <span class="token keyword">TO</span> test<span class="token punctuation">.</span>event_name<span class="token punctuation">;</span>
<span class="token comment">-- 删除事件</span>
<span class="token keyword">drop</span> event event_name<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="MySQL函数"><a href="#MySQL函数" class="headerlink" title="MySQL函数"></a>MySQL函数</h2><h3 id="字符串函数"><a href="#字符串函数" class="headerlink" title="字符串函数"></a>字符串函数</h3><blockquote>
<p>对于针对字符串位置的操作，第一个位置被标记为1。</p>
</blockquote>
<p><strong>ASCII(str)</strong>:返回字符串str的最左面字符的ASCII代码值。如果str是空字符串，返回0。如果str是NULL，返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> ASCII<span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 50</span>
<span class="token keyword">select</span> ASCII<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 50</span>
<span class="token keyword">select</span> ASCII<span class="token punctuation">(</span><span class="token string">'dx'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 100</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>ORD(str)</strong>:如果字符串str最左面字符是一个多字节字符，通过以格式((first byte ASCII code) * 256+(second byte ASCII code))[ * 256+third byte ASCII code…]返回字符的ASCII代码值来返回多字节字符代码。如果最左面的字符不是一个多字节字符。返回与ASCII()函数返回的相同值。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> ORD<span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 50</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>CONV(N,from_base,to_base)</strong>:在不同的数字基之间变换数字。返回数字N的字符串数字，从from_base基变换为to_base基，如果任何参数是NULL，返回NULL。参数N解释为一个整数，但是可以指定为一个整数或一个字符串。最小基是2且最大的基是36。如果to_base是一个负数，N被认为是一个有符号数，否则，N被当作无符号数。CONV以64位点精度工作。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> CONV<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1010'</span>
<span class="token keyword">select</span> CONV<span class="token punctuation">(</span><span class="token string">"6E"</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '172'</span>
<span class="token keyword">select</span> CONV<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">17</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '-H'</span>
<span class="token keyword">select</span> CONV<span class="token punctuation">(</span><span class="token number">10</span><span class="token operator">+</span><span class="token string">"10"</span><span class="token operator">+</span><span class="token string">'10'</span><span class="token operator">+</span><span class="token number">0xa</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '40'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>BIN(N)</strong>:返回二进制值N的一个字符串表示，在此N是一个长整数(BIGINT)数字，这等价于<code>CONV(N,10,2)</code>。如果N是NULL，返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> BIN<span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1100'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>OCT(N)</strong>:返回八进制值N的一个字符串的表示，在此N是一个长整型数字，这等价于<code>CONV(N,10,8)</code>。如果N是NULL，返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> OCT<span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '14'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>HEX(N)</strong>:返回十六进制值N一个字符串的表示，在此N是一个长整型(BIGINT)数字，这等价于<code>CONV(N,10,16)</code>。如果N是NULL，返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> HEX<span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'FF'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>CHAR(N,…)</strong>:CHAR()将参数解释为整数并且返回由这些整数的ASCII代码字符组成的一个字符串。NULL值被跳过。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">CHAR</span><span class="token punctuation">(</span><span class="token number">77</span><span class="token punctuation">,</span><span class="token number">121</span><span class="token punctuation">,</span><span class="token number">83</span><span class="token punctuation">,</span><span class="token number">81</span><span class="token punctuation">,</span><span class="token string">'76'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'MySQL'</span>
<span class="token keyword">select</span> <span class="token keyword">CHAR</span><span class="token punctuation">(</span><span class="token number">77</span><span class="token punctuation">,</span><span class="token number">77.3</span><span class="token punctuation">,</span><span class="token string">'77.3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'MMM'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>CONCAT(str1,str2,…)</strong>:返回来自于参数连结的字符串。如果任何参数是NULL，返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> CONCAT<span class="token punctuation">(</span><span class="token string">'My'</span><span class="token punctuation">,</span> <span class="token string">'S'</span><span class="token punctuation">,</span> <span class="token string">'QL'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'MySQL'</span>
<span class="token keyword">select</span> CONCAT<span class="token punctuation">(</span><span class="token string">'My'</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token string">'QL'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- NULL</span>
<span class="token keyword">select</span> CONCAT<span class="token punctuation">(</span><span class="token number">14.3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '14.3'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>LENGTH(str)，OCTET_LENGTH(str)，CHAR_LENGTH(str)，CHARACTER_LENGTH(str)</strong>,返回字符串str的长度。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> LENGTH<span class="token punctuation">(</span><span class="token string">'text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 4</span>
<span class="token keyword">select</span> OCTET_LENGTH<span class="token punctuation">(</span><span class="token string">'text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 4</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>注意，对于多字节字符，其<code>CHAR_LENGTH()</code>仅计算一次。</p>
<p><strong>LOCATE(substr,str)，POSITION(substr IN str)</strong>,返回子串substr在字符串str第一个出现的位置，如果substr不是在str里面，返回0</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> LOCATE<span class="token punctuation">(</span><span class="token string">'bar'</span><span class="token punctuation">,</span> <span class="token string">'foobarbar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 4</span>
<span class="token keyword">select</span> LOCATE<span class="token punctuation">(</span><span class="token string">'xbar'</span><span class="token punctuation">,</span> <span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>该函数是多字节可靠的。</p>
<p><strong>LOCATE(substr,str,pos)</strong>:返回子串substr在字符串str第一个出现的位置，从位置pos开始。如果substr不是在str里面，返回0。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> LOCATE<span class="token punctuation">(</span><span class="token string">'bar'</span><span class="token punctuation">,</span> <span class="token string">'foobarbar'</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 7</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>这函数是多字节可靠的。</p>
<p><strong>INSTR(str,substr)</strong>:返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同，除了参数被颠倒。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> INSTR<span class="token punctuation">(</span><span class="token string">'foobarbar'</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 4</span>
<span class="token keyword">select</span> INSTR<span class="token punctuation">(</span><span class="token string">'xbar'</span><span class="token punctuation">,</span> <span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>这函数是多字节可靠的。</p>
<p><strong>LPAD(str,len,padstr)</strong>:返回字符串str，左面用字符串padstr填补直到str是len个字符长。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> LPAD<span class="token punctuation">(</span><span class="token string">'hi'</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token string">'??'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '??hi'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>RPAD(str,len,padstr)</strong>:返回字符串str，右面用字符串padstr填补直到str是len个字符长。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> RPAD<span class="token punctuation">(</span><span class="token string">'hi'</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token string">'?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'hi???'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>LEFT(str,len)</strong>:返回字符串str的最左面len个字符。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">LEFT</span><span class="token punctuation">(</span><span class="token string">'foobarbar'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'fooba'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数是多字节可靠的。</p>
<p><strong>RIGHT(str,len)</strong>:返回字符串str的最右面len个字符。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">RIGHT</span><span class="token punctuation">(</span><span class="token string">'foobarbar'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'rbar'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数是多字节可靠的。</p>
<p><strong>SUBSTRING(str,pos,len)，SUBSTRING(str FROM pos FOR len)，MID(str,pos,len)</strong><br>从字符串str返回一个len个字符的子串，从位置pos开始。使用FROM的变种形式是ANSI SQL92语法。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SUBSTRING<span class="token punctuation">(</span><span class="token string">'Quadratically'</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'ratica'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数是多字节可靠的。</p>
<p><strong>SUBSTRING(str,pos)，SUBSTRING(str FROM pos)</strong>:从字符串str的起始位置pos返回一个子串。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SUBSTRING<span class="token punctuation">(</span><span class="token string">'Quadratically'</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'ratically'</span>
<span class="token keyword">select</span> SUBSTRING<span class="token punctuation">(</span><span class="token string">'foobarbar'</span> <span class="token keyword">FROM</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'barbar'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>该函数是多字节可靠的。</p>
<p><strong>SUBSTRING_INDEX(str,delim,count)</strong>:返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数，返回最后的分隔符到左边(从左边数)的所有字符。如果count是负数，返回最后的分隔符到右边的所有字符(从右边数)。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SUBSTRING_INDEX<span class="token punctuation">(</span><span class="token string">'www.mysql.com'</span><span class="token punctuation">,</span> <span class="token string">'.'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'www.mysql'</span>
<span class="token keyword">select</span> SUBSTRING_INDEX<span class="token punctuation">(</span><span class="token string">'www.mysql.com'</span><span class="token punctuation">,</span> <span class="token string">'.'</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'mysql.com'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>LTRIM(str)</strong>:返回删除了其前置空格字符的字符串str。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> LTRIM<span class="token punctuation">(</span><span class="token string">' barbar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'barbar'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>RTRIM(str)</strong>:返回删除了其拖后空格字符的字符串str。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> RTRIM<span class="token punctuation">(</span><span class="token string">'barbar   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'barbar'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)</strong>:返回字符串str，其所有remstr前缀或后缀被删除了。如果没有修饰符BOTH、LEADING或TRAILING给出，BOTH被假定。如果remstr没被指定，空格被删除。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> TRIM<span class="token punctuation">(</span><span class="token string">' bar   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'bar'</span>
<span class="token keyword">select</span> TRIM<span class="token punctuation">(</span>LEADING <span class="token string">'x'</span> <span class="token keyword">FROM</span> <span class="token string">'xxxbarxxx'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'barxxx'</span>
<span class="token keyword">select</span> TRIM<span class="token punctuation">(</span>BOTH <span class="token string">'x'</span> <span class="token keyword">FROM</span> <span class="token string">'xxxbarxxx'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'bar'</span>
<span class="token keyword">select</span> TRIM<span class="token punctuation">(</span>TRAILING <span class="token string">'xyz'</span> <span class="token keyword">FROM</span> <span class="token string">'barxxyz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'barx'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>SOUNDEX(str)</strong>:返回str的一个同音字符串。听起来“大致相同”的2个字符串应该有相同的同音字符串。一个“标准”的同音字符串长是4个字符，但是SOUNDEX()函数返回一个任意长的字符串。你可以在结果上使用SUBSTRING()得到一个“标准”的同音串。所有非数字字母字符在给定的字符串中被忽略。所有在A-Z之外的字符国际字母被当作元音。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SOUNDEX<span class="token punctuation">(</span><span class="token string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'H400'</span>
<span class="token keyword">select</span> SOUNDEX<span class="token punctuation">(</span><span class="token string">'Quadratically'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'Q36324'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>SPACE(N)</strong>:返回由N个空格字符组成的一个字符串。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SPACE<span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '     '</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>REPLACE(str,from_str,to_str)</strong>:返回字符串str，其字符串from_str的所有出现由字符串to_str代替。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">REPLACE</span><span class="token punctuation">(</span><span class="token string">'www.mysql.com'</span><span class="token punctuation">,</span> <span class="token string">'w'</span><span class="token punctuation">,</span> <span class="token string">'Ww'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'WwWwWw.mysql.com'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>REPEAT(str,count)</strong>:返回由重复countTimes次的字符串str组成的一个字符串。如果count&lt;&#x3D;0，返回一个空字符串。如果str或count是NULL，返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">REPEAT</span><span class="token punctuation">(</span><span class="token string">'MySQL'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'MySQLMySQLMySQL'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>REVERSE(str)</strong>:返回颠倒字符顺序的字符串str。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> REVERSE<span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'cba'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数对多字节可靠的。</p>
<p><strong>INSERT(str,pos,len,newstr)</strong>:返回字符串str，在位置pos起始的子串且len个字符长得子串由字符串newstr代替。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">INSERT</span><span class="token punctuation">(</span><span class="token string">'Quadratic'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'What'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'QuWhattic'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>ELT(N,str1,str2,str3,…)</strong>:如果N&#x3D;1，返回str1，如果N&#x3D;2，返回str2，等等。如果N小于1或大于参数个数，返回NULL。ELT()是FIELD()反运算。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> ELT<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'ej'</span><span class="token punctuation">,</span> <span class="token string">'Heja'</span><span class="token punctuation">,</span> <span class="token string">'hej'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'ej'</span>
<span class="token keyword">select</span> ELT<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'ej'</span><span class="token punctuation">,</span> <span class="token string">'Heja'</span><span class="token punctuation">,</span> <span class="token string">'hej'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'foo'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>FIELD(str,str1,str2,str3,…)</strong>:返回str在str1,str2,str3,…清单的索引。如果str没找到，返回0。FIELD()是ELT()反运算。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> FIELD<span class="token punctuation">(</span><span class="token string">'ej'</span><span class="token punctuation">,</span> <span class="token string">'Hej'</span><span class="token punctuation">,</span> <span class="token string">'ej'</span><span class="token punctuation">,</span> <span class="token string">'Heja'</span><span class="token punctuation">,</span> <span class="token string">'hej'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2</span>
<span class="token keyword">select</span> FIELD<span class="token punctuation">(</span><span class="token string">'fo'</span><span class="token punctuation">,</span> <span class="token string">'Hej'</span><span class="token punctuation">,</span> <span class="token string">'ej'</span><span class="token punctuation">,</span> <span class="token string">'Heja'</span><span class="token punctuation">,</span> <span class="token string">'hej'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>FIND_IN_SET(str,strlist)</strong>:如果字符串str在由N子串组成的表strlist之中，返回一个1到N的值。一个字符串表是被“,”分隔的子串组成的一个字符串。如果第一个参数是一个常数字符串并且第二个参数是一种类型为SET的列，FIND_IN_SET()函数被优化而使用位运算！如果str不是在strlist里面或如果strlist是空字符串，返回0。如果任何一个参数是NULL，返回NULL。如果第一个参数包含一个“,”，该函数将工作不正常。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> FIND_IN_SET<span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'a,b,c,d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>MAKE_SET(bits,str1,str2,…)</strong>:返回一个集合(包含由“,”字符分隔的子串组成的一个字符串)，由相应的位在bits集合中的的字符串组成。str1对应于位0，str2对应位1，等等。在str1,str2,…中的NULL串不添加到结果中。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> MAKE_SET<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'a'</span>
<span class="token keyword">SELECT</span> MAKE_SET<span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">|</span> <span class="token number">4</span><span class="token punctuation">,</span><span class="token string">'hello'</span><span class="token punctuation">,</span><span class="token string">'nice'</span><span class="token punctuation">,</span><span class="token string">'world'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'hello,world'</span>
<span class="token keyword">SELECT</span> MAKE_SET<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- ''</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>EXPORT_SET(bits,on,off,[ separator,[number_of_bits] ])</strong>:返回一个字符串，在这里对于在“bits”中设定每一位，你得到一个“on”字符串，并且对于每个复位(reset)的位，你得到一个“off”字符串。每个字符串用“separator”分隔(缺省“,”)，并且只有“bits”的“number_of_bits”(缺省64)位被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> EXPORT_SET<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token string">'Y'</span><span class="token punctuation">,</span><span class="token string">'N'</span><span class="token punctuation">,</span><span class="token string">','</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- Y,N,Y,N</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>LCASE(str),LOWER(str)</strong>:返回字符串str，根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成小写。该函数对多字节是可靠的。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token function">LCASE</span><span class="token punctuation">(</span><span class="token string">'QUADRATICALLY'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'quadratically'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>UCASE(str),UPPER(str)</strong>:返回字符串str，根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成大写。该函数对多字节是可靠的。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token function">UCASE</span><span class="token punctuation">(</span><span class="token string">'Hej'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'HEJ'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>该函数对多字节是可靠的。</p>
<p><strong>LOAD_FILE(file_name)</strong>:读入文件并且作为一个字符串返回文件内容。文件必须在服务器上，你必须指定到文件的完整路径名，而且你必须有file权限。文件必须所有内容都是可读的并且小于max_allowed_packet。如果文件不存在或由于上面原因之一不能被读出，函数返回NULL。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">UPDATE</span> table_name <span class="token keyword">SET</span> blob_column<span class="token operator">=</span>LOAD_FILE<span class="token punctuation">(</span><span class="token string">"/tmp/picture"</span><span class="token punctuation">)</span> <span class="token keyword">WHERE</span> id<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>MySQL必要时自动变换数字为字符串，并且反过来也如此：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token operator">+</span><span class="token string">"1"</span><span class="token punctuation">;</span><span class="token comment">-- 2</span>
<span class="token keyword">SELECT</span> CONCAT<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">' test'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '2 test'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>如果你想要明确地变换一个数字到一个字符串，把它作为参数传递到CONCAT()。如果字符串函数提供一个二进制字符串作为参数，结果字符串也是一个二进制字符串。被变换到一个字符串的数字被当作是一个二进制字符串。这仅影响比较</p>
<p><strong>COMPRESS(string_to_compress)</strong>:压缩一个字符串。这个函数要求MySQL已经用一个诸如zlib的压缩库压缩过。否则，返回值始终是NULL。<strong>UNCOMPRESS()</strong> 可将压缩过的字符串进行解压缩。</p>
<p><strong>CONCAT_WS(separator ,str1 ,str2 ,…)</strong>:CONCAT_WS()代表CONCAT With Separator，是CONCAT()的特殊形式。第一个参数是其它参数的分隔符，分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串，也可以是其它参数。如果分隔符为NULL，则结果为NULL。函数会忽略任何分隔符参数后的NULL值。</p>
<p><strong>FORMAT(X ,D)</strong>:将number X设置为格式’#,###,###.##’,以四舍五入的方式保留到小数点后D位,而返回结果为一个字符串。</p>
<p><strong>QUOTE(str)</strong>:引证一个字符串，由此产生一个在SQL语句中可用作完全转义数据值的结果。 返回的字符串由单引号标注，每例都带有单引号(‘’’)、反斜线符号(‘\’)、ASCII NUL以及前面有反斜线符号的Control-Z。如果自变量的值为NULL,则返回不带单引号的单词“NULL”。</p>
<p><strong>UNCOMPRESSED_LENGTH(compressed_string)</strong>:返回压缩字符串压缩前的长度。</p>
<p><strong>UNHEX(str)</strong>:执行从HEX(str)的反向操作。就是说，它将参数中的每一对十六进制数字理解为一个数字，并将其转化为该数字代表的字符。结果字符以二进制字符串的形式返回</p>
<h3 id="时间函数"><a href="#时间函数" class="headerlink" title="时间函数"></a>时间函数</h3><p>这里是一个使用日期函数的例子。下面的查询选择了所有记录，其date_col的值是在最后30天以内：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> something <span class="token keyword">FROM</span> <span class="token keyword">table</span> <span class="token keyword">WHERE</span> TO_DAYS<span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> TO_DAYS<span class="token punctuation">(</span>date_col<span class="token punctuation">)</span> <span class="token operator">&lt;=</span> <span class="token number">30</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>DAYOFWEEK(date)</strong>:返回日期date的星期索引(1&#x3D;星期天，2&#x3D;星期一,……7&#x3D;星期六)。这些索引值对应于ODBC标准。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DAYOFWEEK<span class="token punctuation">(</span><span class="token string">'1998-02-03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 3</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>WEEKDAY(date)</strong>:返回date的星期索引(0&#x3D;星期一，1&#x3D;星期二,……6&#x3D;星期天)。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> WEEKDAY<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 5</span>
<span class="token keyword">select</span> WEEKDAY<span class="token punctuation">(</span><span class="token string">'1997-11-05'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>DAYOFMONTH(date)</strong>:返回date的月份中日期，在1到31范围内。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DAYOFMONTH<span class="token punctuation">(</span><span class="token string">'1998-02-03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 3</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>DAYOFYEAR(date)</strong>:返回date在一年中的日数,在1到366范围内。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DAYOFYEAR<span class="token punctuation">(</span><span class="token string">'1998-02-03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 34</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>MONTH(date)</strong>:返回date的月份，范围1到12。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">MONTH</span><span class="token punctuation">(</span><span class="token string">'1998-02-03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>DAYNAME(date)</strong>:返回date的星期名字。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DAYNAME<span class="token punctuation">(</span><span class="token string">"1998-02-05"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'Thursday'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>MONTHNAME(date)</strong>:返回date的月份名字。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> MONTHNAME<span class="token punctuation">(</span><span class="token string">"1998-02-05"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'February'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>QUARTER(date)</strong>:返回date一年中的季度，范围1到4。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> QUARTER<span class="token punctuation">(</span><span class="token string">'98-04-01'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>WEEK(date),WEEK(date,first)</strong>:对于星期天是一周的第一天的地方，有一个单个参数，返回date的周数，范围在0到52。2个参数形式WEEK()允许你指定星期是否开始于星期天或星期一。如果第二个参数是0，星期从星期天开始，如果第二个参数是1，从星期一开始。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> WEEK<span class="token punctuation">(</span><span class="token string">'1998-02-20'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 7</span>
<span class="token keyword">select</span> WEEK<span class="token punctuation">(</span><span class="token string">'1998-02-20'</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 7</span>
<span class="token keyword">select</span> WEEK<span class="token punctuation">(</span><span class="token string">'1998-02-20'</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 8</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>YEAR(date)</strong>:返回date的年份，范围在1000到9999。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">YEAR</span><span class="token punctuation">(</span><span class="token string">'98-02-03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1998</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>HOUR(time)</strong>:返回time的小时，范围是0到23。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">HOUR</span><span class="token punctuation">(</span><span class="token string">'10:05:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 10</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>MINUTE(time)</strong>:返回time的分钟，范围是0到59。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">MINUTE</span><span class="token punctuation">(</span><span class="token string">'98-02-03 10:05:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 5</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>SECOND(time)</strong>:回来time的秒数，范围是0到59。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token keyword">SECOND</span><span class="token punctuation">(</span><span class="token string">'10:05:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 3</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>PERIOD_ADD(P,N)</strong>:增加N个月到阶段P(以格式YYMM或YYYYMM)。以格式YYYYMM返回值。注意阶段参数P不是日期值。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> PERIOD_ADD<span class="token punctuation">(</span><span class="token number">9801</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 199803</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>PERIOD_DIFF(P1,P2)</strong>:返回在时期P1和P2之间月数，P1和P2应该以格式YYMM或YYYYMM。注意，时期参数P1和P2不是日期值。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">　　mysql<span class="token operator">></span> <span class="token keyword">select</span> PERIOD_DIFF<span class="token punctuation">(</span><span class="token number">9802</span><span class="token punctuation">,</span><span class="token number">199703</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 11</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>DATE_ADD(date,INTERVAL expr type),DATE_SUB(date,INTERVAL expr type)，ADDDATE(date,INTERVAL expr type),SUBDATE(date,INTERVAL expr type)</strong>,这些功能执行日期运算。对于MySQL3.22，他们是新的。ADDDATE()和SUBDATE()是DATE_ADD()和DATE_SUB()的同义词。在MySQL3.23中，你可以使用+和-而不是DATE_ADD()和DATE_SUB()。（见例子）date是一个指定开始日期的DATETIME或DATE值，expr是指定加到开始日期或从开始日期减去的间隔值一个表达式，expr是一个字符串；它可以以一个“-”开始表示负间隔。type是一个关键词，指明表达式应该如何被解释。EXTRACT(type FROM date)函数从日期中返回“type”间隔。下表显示了type和expr参数怎样被关联</p>
<table>
<thead>
<tr>
<th>type值</th>
<th>含义</th>
<th>期望的expr格式</th>
</tr>
</thead>
<tbody><tr>
<td>SECOND</td>
<td>秒</td>
<td>SECONDS</td>
</tr>
<tr>
<td>MINUTE</td>
<td>分钟</td>
<td>MINUTES</td>
</tr>
<tr>
<td>HOUR</td>
<td>时间</td>
<td>HOURS</td>
</tr>
<tr>
<td>DAY</td>
<td>天</td>
<td>DAYS</td>
</tr>
<tr>
<td>MONTH</td>
<td>月</td>
<td>MONTHS</td>
</tr>
<tr>
<td>YEAR</td>
<td>年</td>
<td>YEARS</td>
</tr>
<tr>
<td>MINUTE_SECOND</td>
<td>分钟和秒</td>
<td>“MINUTES:SECONDS”</td>
</tr>
<tr>
<td>HOUR_MINUTE</td>
<td>小时和分钟</td>
<td>“HOURS:MINUTES”</td>
</tr>
<tr>
<td>DAY_HOUR</td>
<td>天和小时</td>
<td>“DAYS HOURS”</td>
</tr>
<tr>
<td>YEAR_MONTH</td>
<td>年和月</td>
<td>“YEARS-MONTHS”</td>
</tr>
<tr>
<td>HOUR_SECOND</td>
<td>小时, 分钟</td>
<td>“HOURS:MINUTES:SECONDS”</td>
</tr>
<tr>
<td>DAY_MINUTE</td>
<td>天, 小时, 分钟</td>
<td>“DAYS HOURS:MINUTES”</td>
</tr>
<tr>
<td>DAY_SECOND</td>
<td>天, 小时, 分钟, 秒</td>
<td>“DAYS HOURS:MINUTES:SECONDS”</td>
</tr>
</tbody></table>
<p>MySQL在expr格式中允许任何标点分隔符。表示显示的是建议的分隔符。如果date参数是一个DATE值并且你的计算仅仅包含YEAR、MONTH和DAY部分(即，没有时间部分)，结果是一个DATE值。否则结果是一个DATETIME值。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token string">"1997-12-31 23:59:59"</span> <span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">SECOND</span><span class="token punctuation">;</span><span class="token comment">-- 1998-01-01 00:00:00</span>
<span class="token keyword">SELECT</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span> <span class="token operator">+</span> <span class="token string">"1997-12-31"</span><span class="token punctuation">;</span><span class="token comment">-- 1998-01-01</span>
<span class="token keyword">SELECT</span> <span class="token string">"1998-01-01"</span> <span class="token operator">-</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">SECOND</span><span class="token punctuation">;</span><span class="token comment">-- 1997-12-31 23:59:59</span>
<span class="token keyword">SELECT</span> DATE_ADD<span class="token punctuation">(</span><span class="token string">"1997-12-31 23:59:59"</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">SECOND</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1998-01-01 00:00:00</span>
<span class="token keyword">SELECT</span> DATE_ADD<span class="token punctuation">(</span><span class="token string">"1997-12-31 23:59:59"</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1998-01-01 23:59:59</span>
<span class="token keyword">SELECT</span> DATE_ADD<span class="token punctuation">(</span><span class="token string">"1997-12-31 23:59:59"</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token string">"1:1"</span> MINUTE_SECOND<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1998-01-01 00:01:00</span>
<span class="token keyword">SELECT</span> DATE_SUB<span class="token punctuation">(</span><span class="token string">"1998-01-01 00:00:00"</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token string">"1 1:1:1"</span> DAY_SECOND<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1997-12-30 22:58:59</span>
<span class="token keyword">SELECT</span> DATE_ADD<span class="token punctuation">(</span><span class="token string">"1998-01-01 00:00:00"</span><span class="token punctuation">,</span> <span class="token keyword">INTERVAL</span> <span class="token string">"-1 10"</span> DAY_HOUR<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1997-12-30 14:00:00</span>
<span class="token keyword">SELECT</span> DATE_SUB<span class="token punctuation">(</span><span class="token string">"1998-01-02"</span><span class="token punctuation">,</span> <span class="token keyword">INTERVAL</span> <span class="token number">31</span> <span class="token keyword">DAY</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1997-12-02</span>
<span class="token keyword">SELECT</span> EXTRACT<span class="token punctuation">(</span><span class="token keyword">YEAR</span> <span class="token keyword">FROM</span> <span class="token string">"1999-07-02"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1999</span>
<span class="token keyword">SELECT</span> EXTRACT<span class="token punctuation">(</span>YEAR_MONTH <span class="token keyword">FROM</span> <span class="token string">"1999-07-02 01:02:03"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 199907</span>
<span class="token keyword">SELECT</span> EXTRACT<span class="token punctuation">(</span>DAY_MINUTE <span class="token keyword">FROM</span> <span class="token string">"1999-07-02 01:02:03"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 20102</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>如果你指定太短的间隔值(不包括type关键词期望的间隔部分)，MySQL假设你省掉了间隔值的最左面部分。例如，如果你指定一个type是DAY_SECOND，值expr被希望有天、小时、分钟和秒部分。如果你象”1:10”这样指定值，MySQL假设日子和小时部分是丢失的并且值代表分钟和秒。换句话说，”1:10” DAY_SECOND以它等价于”1:10” MINUTE_SECOND的方式解释，这对那MySQL解释TIME值表示经过的时间而非作为一天的时间的方式有二义性。如果你使用确实不正确的日期，结果是NULL。如果你增加MONTH、YEAR_MONTH或YEAR并且结果日期大于新月份的最大值天数，日子在新月用最大的天调整。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DATE_ADD<span class="token punctuation">(</span><span class="token string">'1998-01-30'</span><span class="token punctuation">,</span> <span class="token keyword">Interval</span> <span class="token number">1</span> <span class="token keyword">month</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 1998-02-28</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>注意，从前面的例子中词INTERVAL和type关键词不是区分大小写的。<br>　　<br><strong>TO_DAYS(date)</strong>:给出一个日期date，返回一个天数(从0年的天数)。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> TO_DAYS<span class="token punctuation">(</span><span class="token number">950501</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 728779</span>
<span class="token keyword">select</span> TO_DAYS<span class="token punctuation">(</span><span class="token string">'1997-10-07'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 729669</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>TO_DAYS()不打算用于使用格列高里历(1582)出现前的值。</p>
<p><strong>FROM_DAYS(N)</strong>:给出一个天数N，返回一个DATE值。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> FROM_DAYS<span class="token punctuation">(</span><span class="token number">729669</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1997-10-07'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>DATE_FORMAT(date,format)</strong>:根据format字符串格式化date值。下列修饰符可以被用在format字符串中:</p>
<ul>
<li>%M 月名字(January……December)</li>
<li>%W 星期名字(Sunday……Saturday)</li>
<li>%D 有英语前缀的月份的日期(1st,2nd,3rd,等等。)</li>
<li>%Y 年,数字,4位</li>
<li>%y 年,数字,2位</li>
<li>%a 缩写的星期名字(Sun……Sat)</li>
<li>%d 月份中的天数,数字(00……31)</li>
<li>%e 月份中的天数,数字(0……31)</li>
<li>%m 月,数字(01……12)</li>
<li>%c 月,数字(1……12)</li>
<li>%b 缩写的月份名字(Jan……Dec)</li>
<li>%j 一年中的天数(001……366)</li>
<li>%H 小时(00……23)</li>
<li>%k 小时(0……23)</li>
<li>%h 小时(01……12)</li>
<li>%I 小时(01……12)</li>
<li>%l 小时(1……12)</li>
<li>%i 分钟,数字(00……59)</li>
<li>%r 时间,12小时(hh:mm:ss [AP]M)</li>
<li>%T 时间,24小时(hh:mm:ss)</li>
<li>%S 秒(00……59)</li>
<li>%s 秒(00……59)</li>
<li>%p AM或PM</li>
<li>%w 一个星期中的天数(0&#x3D;Sunday ……6&#x3D;Saturday)</li>
<li>%U 星期(0……52),这里星期天是星期的第一天</li>
<li>%u 星期(0……52),这里星期一是星期的第一天</li>
<li>%% 一个文字“%”。</li>
</ul>
<p>所有的其他字符不做解释被复制到结果中。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> DATE_FORMAT<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">,</span> <span class="token string">'%W %M %Y'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 'Saturday October 1997'</span>
<span class="token keyword">select</span> DATE_FORMAT<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">,</span> <span class="token string">'%H:%i:%s'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '22:23:00'</span>
<span class="token keyword">select</span> DATE_FORMAT<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">,</span><span class="token string">'%D %y %a %d %m %b %j'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '4th 97 Sat 04 10 Oct 277'</span>
<span class="token keyword">select</span> DATE_FORMAT<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">,</span><span class="token string">'%H %k %I %r %T %S %w'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '22 22 10 10:23:00 PM 22:23:00 00 6'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>MySQL3.23中，在格式修饰符字符前需要%。在MySQL更早的版本中，%是可选的。</p>
<p><strong>TIME_FORMAT(time,format)</strong>:这像上面的DATE_FORMAT()函数一样使用，但是format字符串只能包含处理小时、分钟和秒的那些格式修饰符。其他修饰符产生一个NULL值或0。<br>　　<br><strong>CURDATE(),CURRENT_DATE</strong>:以’YYYY-MM-DD’或YYYYMMDD格式返回今天日期值，取决于函数是在一个字符串还是数字上下文被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1997-12-15'</span>
<span class="token keyword">select</span> CURDATE<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">-- 19971215</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>CURTIME(),CURRENT_TIME</strong>:以’HH:MM:SS’或HHMMSS格式返回当前时间值，取决于函数是在一个字符串还是在数字的上下文被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> CURTIME<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '23:50:26'</span>
<span class="token keyword">select</span> CURTIME<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">-- 235026</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>NOW(),SYSDATE(),CURRENT_TIMESTAMP</strong>,以’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS格式返回当前的日期和时间，取决于函数是在一个字符串还是在数字的上下文被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1997-12-15 23:50:26'</span>
<span class="token keyword">select</span> <span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">-- 19971215235026</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>UNIX_TIMESTAMP(),UNIX_TIMESTAMP(date)</strong>:如果没有参数调用，返回一个Unix时间戳记(从’1970-01-01 00:00:00’GMT开始的秒数)。如果UNIX_TIMESTAMP()用一个date参数被调用，它返回从’1970-01-01 00:00:00’GMT开始的秒数值。date可以是一个DATE字符串、一个DATETIME字符串、一个TIMESTAMP或以YYMMDD或YYYYMMDD格式的本地时间的一个数字。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> UNIX_TIMESTAMP<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 882226357</span>
<span class="token keyword">select</span> UNIX_TIMESTAMP<span class="token punctuation">(</span><span class="token string">'1997-10-04 22:23:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 875996580</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>当UNIX_TIMESTAMP被用于一个TIMESTAMP列，函数将直接接受值，没有隐含的“string-to-unix-timestamp”变换。</p>
<p><strong>FROM_UNIXTIME(unix_timestamp)</strong>:以’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS格式返回unix_timestamp参数所表示的值，取决于函数是在一个字符串还是或数字上下文中被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> FROM_UNIXTIME<span class="token punctuation">(</span><span class="token number">875996580</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1997-10-04 22:23:00'</span>
<span class="token keyword">select</span> FROM_UNIXTIME<span class="token punctuation">(</span><span class="token number">875996580</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">-- 19971004222300</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>FROM_UNIXTIME(unix_timestamp,format)</strong>:返回表示Unix时间标记的一个字符串，根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> FROM_UNIXTIME<span class="token punctuation">(</span>UNIX_TIMESTAMP<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'%Y %D %M %h:%i:%s %x'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '1997 23rd December 03:43:30 x'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>SEC_TO_TIME(seconds)</strong>:返回seconds参数，变换成小时、分钟和秒，值以’HH:MM:SS’或HHMMSS格式化，取决于函数是在一个字符串还是在数字上下文中被使用。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> SEC_TO_TIME<span class="token punctuation">(</span><span class="token number">2378</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- '00:39:38'</span>
<span class="token keyword">select</span> SEC_TO_TIME<span class="token punctuation">(</span><span class="token number">2378</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">-- 3938</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>TIME_TO_SEC(time)</strong>:返回time参数，转换成秒。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> TIME_TO_SEC<span class="token punctuation">(</span><span class="token string">'22:23:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 80580</span>
<span class="token keyword">select</span> TIME_TO_SEC<span class="token punctuation">(</span><span class="token string">'00:39:38'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- 2378</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>Mysql取系统函数：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">Select</span> curtime<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">Select</span> curdate<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">Select</span> sysdate<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>ADDDATE(date ,INTERVAL expr type ),ADDDATE(expr ,days)</strong>:当被第二个参数的INTERVAL格式激活后，ADDDATE()就是DATE_ADD()的同义词。相关函数SUBDATE()则是DATE_SUB()的同义词。对于INTERVAL参数上的信息，请参见关于DATE_ADD()的论述。</p>
<p><strong>ADDTIME(expr ,expr2)</strong>:ADDTIME()将expr2添加至expr然后返回结果。expr是一个时间或时间日期表达式，而expr2是一个时间表达式。</p>
<p><strong>CONVERT_TZ(dt ,from_tz ,to_tz)</strong>:将时间日期值dt从from_tz给出的时区转到to_tz给出的时区，然后返回结果值。关于可能指定的时区的详细论述，若自变量无效，则这个函数会返回NULL</p>
<p><strong>DATE(expr)</strong>:提取日期或时间日期表达式expr中的日期部分。</p>
<p><strong>DATEDIFF(expr,expr2)</strong>:返回起始时间expr和结束时间expr2之间的天数。Expr和expr2为日期或date-and-time表达式。计算中只用到这些值的日期部分。</p>
<p><strong>DATE_ADD(date ,INTERVAL expr type ),DATE_SUB(date ,INTERVAL expr type)</strong>:这些函数执行日期运算。date是一个DATETIME或DATE值，用来指定起始时间。expr是一个表达式，用来指定从起始日期添加或减去的时间间隔值。Expr是一个字符串;对于负值的时间间隔，它可以以一个‘-’开头。type为关键词，它指示了表达式被解释的方式。</p>
<p><strong>DAY(date)</strong>:DAY()和DAYOFMONTH()的意义相同</p>
<p><strong>EXTRACT(type FROM date)</strong>:函数所使用的时间间隔类型说明符同DATE_ADD()或DATE_SUB()的相同,但它从日期中提取其部分，而不是执行日期运算。</p>
<p><strong>GET_FORMAT(DATE|TIME|DATETIME, ‘EUR’|’USA’|’JIS’|’ISO’|’INTERNAL’)</strong>:返回一个格式字符串。这个函数在同DATE_FORMAT()及STR_TO_DATE()函数结合时很有用</p>
<p><strong>LAST_DAY(date)</strong>:获取一个日期或日期时间值，返回该月最后一天对应的值。若参数无效，则返回NULL。</p>
<p><strong>LOCALTIME,LOCALTIME()</strong>:LOCALTIME及LOCALTIME()和NOW()具有相同意义。</p>
<p><strong>LOCALTIMESTAMP, LOCALTIMESTAMP()</strong>:LOCALTIMESTAMP和LOCALTIMESTAMP()和NOW()具有相同意义。</p>
<p><strong>MAKEDATE(year ,dayofyear)</strong>:给出年份值和一年中的天数值，返回一个日期。dayofyear必须大于0，否则结果为NULL。</p>
<p><strong>MAKETIME(hour ,minute ,second)</strong>:返回由hour、minute和second参数计算得出的时间值</p>
<p><strong>CROSECOND(expr)</strong>:从时间或日期时间表达式expr返回微秒值，其数字范围从0到999999。</p>
<p><strong>STR_TO_DATE(str,format)</strong>:这是DATE_FORMAT()函数的倒转。它获取一个字符串str和一个格式字符串format。若格式字符串包含日期和时间部分，则STR_TO_DATE()返回一个DATETIME值，若该字符串只包含日期部分或时间部分，则返回一个DATE或TIME值。</p>
<p><strong>SUBDATE(date ,INTERVAL expr type),SUBDATE(expr,days)</strong>:当被第二个参数的INTERVAL型式调用时,SUBDATE()和DATE_SUB()的意义相同。对于有关INTERVAL参数的信息，见有关DATE_ADD()的讨论。</p>
<p><strong>SUBTIME(expr,expr2)</strong>:SUBTIME()从expr中提取expr2,然后返回结果。expr是一个时间或日期时间表达式，而xpr2是一个时间表达式。</p>
<p><strong>SYSDATE()</strong>:返回当前日期和时间值，格式为’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS，具体格式根据函数是否用在字符串或数字语境而定。</p>
<p><strong>TIME(expr)</strong>:提取一个时间或日期时间表达式的时间部分，并将其以字符串形式返回。</p>
<p><strong>TIMEDIFF(expr ,expr2)</strong>:返回起始时间expr和结束时间expr2之间的时间。expr和expr2为时间或date-and-time表达式 , 两个的类型必须一样。</p>
<p><strong>TIMESTAMP(expr) , TIMESTAMP(expr ,expr2)</strong>:对于一个单参数,该函数将日期或日期时间表达式expr作为日期时间值返回，对于两个参数,它将时间表达式expr2添加到日期或日期时间表达式expr中，将theresult作为日期时间值返回。</p>
<p><strong>TIMESTAMPADD(interval ,int_expr ,datetime_expr)</strong>:将整型表达式int_expr添加到日期或日期时间表达式datetime_expr中。int_expr的单位被时间间隔参数给定，该参数必须是以下值的其中一个：FRAC_SECOND、SECOND、MINUTE、HOUR、DAY、WEEK、MONTH、QUARTER或YEAR。可使用所显示的关键词指定Interval值，或使用SQL_TSI_前缀。例如,DAY或SQL_TSI_DAY都是正确的</p>
<p><strong>TIMESTAMPDIFF(interval ,datetime_expr1 ,datetime_expr2)</strong>:返回日期或日期时间表达式datetime_expr1和datetime_expr2之间的整数差。其结果的单位由interval参数给出。interval的法定值同TIMESTAMPADD()函数说明中所列出的相同。</p>
<p><strong>UTC_DATE()</strong>:返回当前UTC日期值，其格式为’YYYY-MM-DD’或YYYYMMDD，具体格式取决于函数是否用在字符串或数字语境中。</p>
<p><strong>UTC_TIME()</strong>:返回当前UTC值，其格式为’HH:MM:SS’或HHMMSS，具体格式根据该函数是否用在字符串或数字语境而定。</p>
<p><strong>UTC_TIMESTAMP()</strong>:返回当前UTC日期及时间值，格式为’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS，具体格式根据该函数是否用在字符串或数字语境而定</p>
<p><strong>WEEKOFYEAR(date)</strong>:将该日期的阳历周以数字形式返回，范围是从1到53。它是一个兼容度函数，相当于WEEK(date ,3)。</p>
<p><strong>YEARWEEK(date), YEARWEEK(date,start)</strong>:返回一个日期对应的年或周。start参数的工作同start参数对WEEK()的工作相同。结果中的年份可以和该年的第一周和最后一周对应的日期参数有所不同。</p>
<h3 id="控制流程函数"><a href="#控制流程函数" class="headerlink" title="控制流程函数"></a>控制流程函数</h3><p><strong>CASE WHEN THEN函数</strong></p>
<p>语法：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CASE</span> <span class="token keyword">value</span> <span class="token keyword">WHEN</span> <span class="token punctuation">[</span>compare<span class="token operator">-</span><span class="token keyword">value</span><span class="token punctuation">]</span> <span class="token keyword">THEN</span> result <span class="token punctuation">[</span><span class="token keyword">WHEN</span> <span class="token punctuation">[</span>compare<span class="token operator">-</span><span class="token keyword">value</span><span class="token punctuation">]</span> <span class="token keyword">THEN</span> result ……<span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token keyword">ELSE</span> result <span class="token punctuation">]</span> <span class="token keyword">END</span> <span class="token keyword">CASE</span> <span class="token keyword">WHEN</span> <span class="token punctuation">[</span>condition<span class="token punctuation">]</span> <span class="token keyword">THEN</span> result <span class="token punctuation">[</span><span class="token keyword">WHEN</span><span class="token punctuation">[</span>condition<span class="token punctuation">]</span> <span class="token keyword">THEN</span> result ……<span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token keyword">ELSE</span> result<span class="token punctuation">]</span> <span class="token keyword">END</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>函数用法说明：在第一个方案的返回结果中，value&#x3D;compare-value。而第二个方案的返回结果是第一种情况的真实结果。如果没有匹配的结果值，则返回结果为ELSE后的结果，如果没有ELSE部分，则返回值为NULL</p>
<p><strong>IF函数用法</strong></p>
<p>语法：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">IF</span><span class="token punctuation">(</span>expr1<span class="token punctuation">,</span>expr2<span class="token punctuation">,</span>expr3<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>函数用法说明：如果expr1是TRUE(expr1 &lt;&gt; 0 and expr1 &lt;&gt; NULL) ，则IF()的返回值为expr2;否则返回值则为expr3。IF()的返回值为数字值或字符串值，具体情况视其所在语境而定</p>
<p><strong>IFNULL函数</strong></p>
<p>语法：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">IFNULL<span class="token punctuation">(</span>expr1<span class="token punctuation">,</span>expr2<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>函数用法说明：假如expr1不为NULL，则IFNULL()的返回值为expr1;否则其返回值为expr2。IFNULL()的返回值是数字或是字符串，具体情况取决于其所使用的语境</p>
<h3 id="数学函数"><a href="#数学函数" class="headerlink" title="数学函数"></a>数学函数</h3><p><strong>ABS(X)</strong>:返回X的绝对值</p>
<p><strong>ACOS(X)</strong>:返回X反余弦,即,余弦是X的值。若X不在-1到1的范围之内，则返回NULL。</p>
<p><strong>ASIN(X)</strong>:返回X的反正弦，即，正弦为X的值。若X不在-1到1的范围之内，则返回NULL。</p>
<p><strong>ATAN(X)</strong>:返回X的反正切，即，正切为X的值。</p>
<p><strong>ATAN(Y,X),ATAN2(Y,X)</strong>:返回两个变量X及Y的反正切。它类似于Y或X的反正切计算, 除非两个参数的符号均用于确定结果所在象限。</p>
<p><strong>CEILING(X),CEIL(X)</strong>:返回不小于X的最小整数值。</p>
<p><strong>COS(X)</strong>:返回X的余弦，其中X在弧度上已知。</p>
<p><strong>COT(X)</strong>:返回X的余切</p>
<p><strong>CRC32(expr)</strong>:计算循环冗余码校验值并返回一个32比特无符号值。若参数为NULL，则结果为NULL。该参数应为一个字符串，而且在不是字符串的情况下会被作为字符串处理（若有可能）</p>
<p><strong>DEGREES(X)</strong>:返回参数X,该参数由弧度被转化为度。</p>
<p><strong>EXP(X)</strong>:返回e的X乘方后的值(自然对数的底)。</p>
<p><strong>FLOOR(X)</strong>:返回不大于X的最大整数值。</p>
<p><strong>FORMAT(X,D)</strong>:将数字X的格式写成’#,###,###.##’格式,即保留小数点后D位，而第D位的保留方式为四舍五入，然后将结果以字符串的形式返回</p>
<p><strong>LN(X)</strong>:返回X的自然对数,即X相对于基数e的对数</p>
<p><strong>LOG(X),LOG(B,X)</strong>:若用一个参数调用，这个函数就会返回X的自然对数。</p>
<p><strong>LOG2(X)</strong>:返回X的基数为2的对数。</p>
<p><strong>LOG10(X)</strong>:返回X的基数为10的对数。</p>
<p><strong>MOD(N,M),N%M,N MOD M</strong>:模操作。返回N被M除后的余数。</p>
<p><strong>PI()</strong>:返回ϖ(pi)的值。默认的显示小数位数是7位,然而MySQL内部会使用完全双精度值。</p>
<p><strong>POW(X,Y),POWER(X,Y)</strong>:返回X的Y乘方的结果值。</p>
<p><strong>RADIANS(X)</strong>:返回由度转化为弧度的参数X, (注意ϖ弧度等于180度)。</p>
<p><strong>RAND(),RAND(N)</strong>:返回一个随机浮点值v，范围在0到1之间(即,其范围为0 ≤ v ≤ 1.0)。若已指定一个整数参数N，则它被用作种子值，用来产生重复序列。</p>
<p><strong>ROUND(X),ROUND(X,D)</strong>:返回参数X,其值接近于最近似的整数。在有两个参数的情况下，返回X，其值保留到小数点后D位，而第D位的保留方式为四舍五入。若要接保留X值小数点左边的D位，可将D设为负值。</p>
<p><strong>SIGN(X)</strong>:返回参数作为-1、0或1的符号，该符号取决于X的值为负、零或正。</p>
<p><strong>SIN(X)</strong>:返回X正弦，其中X在弧度中被给定。</p>
<p><strong>SQRT(X)</strong>:返回非负数X的二次方根。</p>
<p><strong>TAN(X)</strong>:返回X的正切，其中X在弧度中被给定。</p>
<p><strong>TRUNCATE(X,D)</strong>:返回被舍去至小数点后D位的数字X。若D的值为0,则结果不带有小数点或不带有小数部分。可以将D设为负数,若要截去(归零)X小数点左起第D位开始后面所有低位的值</p>
<h3 id="全文搜索功能函数"><a href="#全文搜索功能函数" class="headerlink" title="全文搜索功能函数"></a>全文搜索功能函数</h3><p>MATCH (col1,col2,…)<br>AGAINST (expr [IN BOOLEAN MODE | WITH QUERY EXPANSION])</p>
<h3 id="加密函数"><a href="#加密函数" class="headerlink" title="加密函数"></a>加密函数</h3><p><strong>AES_ENCRYPT(str,key_str),AES_DECRYPT(crypt_str,key_str)</strong>:这些函数允许使用官方AES进行加密和数据加密(高级加密标准)算法,即以前人们所熟知的“Rijndael”。保密关键字的长度为128比特，不过你可以通过改变源而将其延长到256比特。我们选择了128比特的原因是它的速度要快得多，且对于大多数用途而言这个保密程度已经够用。</p>
<p><strong>DECODE(crypt_str,pass_str)</strong>:使用pass_str作为密码，解密加密字符串crypt_str，crypt_str应该是由ENCODE()返回的字符串。</p>
<p><strong>ENCODE(str,pass_str)</strong>:使用pass_str作为密码，解密str。使用DECODE()解密结果。</p>
<p><strong>DES_DECRYPT(crypt_str [,key_str ])</strong>:使用DES_ENCRYPT()加密一个字符串。若出现错误，这个函数会返回NULL。</p>
<p><strong>DES_ENCRYPT(str [,(key_num |key_str )])</strong>:用Triple-DES算法给出的关键字加密字符串。若出现错误，这个函数会返回NULL</p>
<p><strong>ENCRYPT(str [,salt ])</strong>:使用Unix crypt()系统调用加密str。salt参数应为一个至少包含2个字符的字符串。若没有给出salt参数，则使用任意值。</p>
<p><strong>MD5(str)</strong>:为字符串算出一个MD5 128比特检查和。该值以32位十六进制数字的二进制字符串的形式返回,若参数为NULL则会返回NULL。例如，返回值可被用作散列关键字</p>
<p><strong>OLD_PASSWORD(str)</strong>:当PASSWORD()的执行变为改善安全性时，OLD_PASSWORD()会被添加到MySQL。OLD_PASSWORD()返回从前的PASSWORD()执行值(4.1之前)，同时允许你为任何4.1之前的需要连接到你的5.1版本MySQL服务器前客户端设置密码，从而不至于将它们切断</p>
<p><strong>PASSWORD(str)</strong>:从原文密码str计算并返回密码字符串，当参数为NULL时返回NULL。这个函数用于用户授权表的Password列中的加密MySQL密码存储</p>
<h3 id="信息函数"><a href="#信息函数" class="headerlink" title="信息函数"></a>信息函数</h3><p><strong>BENCHMARK(count,expr)</strong>:函数重复count次执行表达式expr。它可以被用于计算MySQL处理表达式的速度。结果值通常为0 。另一种用处来自mysql客户端内部,能够报告问询执行的次数</p>
<p><strong>CHARSET(str)</strong>:返回字符串自变量的字符集。</p>
<p><strong>COERCIBILITY(str)</strong>:返回字符串自变量的整序可压缩性值。</p>
<p><strong>COLLATION(str)</strong>:返回惠字符串参数的排序方式。</p>
<p><strong>CONNECTION_ID()</strong>:返回对于连接的连接ID(线程ID)。每个连接都有各自的唯一ID。</p>
<p><strong>CURRENT_USER,CURRENT_USER()</strong>:返回当前话路被验证的用户名和主机名组合。这个值符合确定你的存取权限的MySQL账户。在被指定SQL SECURITY DEFINER特征的存储程序内，CURRENT_USER()返回程序的创建者</p>
<p><strong>DATABASE()</strong>:返回使用utf8字符集的默认(当前)数据库名。在存储程序里，默认数据库是同该程序向关联的数据库，但并不一定与调用语境的默认数据库相同。</p>
<p><strong>FOUND_ROWS()</strong>:SELECT语句可能包括一个LIMIT子句，用来限制服务器返回客户端的行数。在有些情况下，需要不用再次运行该语句而得知在没有LIMIT时到底该语句返回了多少行。为了知道这个行数,包括在SELECT语句中选择SQL_CALC_FOUND_ROWS，随后调用FOUND_ROWS()</p>
<p><strong>LAST_INSERT_ID(),LAST_INSERT_ID(expr)</strong>:自动返回最后一个INSERT或UPDATE问询为AUTO_INCREMENT列设置的第一个发生的值。</p>
<p><strong>ROW_COUNT()</strong>:返回被前面语句升级的、插入的或删除的行数。这个行数和mysql客户端显示的行数及mysql_affected_rows() C API函数返回的值相同。</p>
<p><strong>SCHEMA()</strong>:这个函数和DATABASE()具有相同的意义</p>
<p><strong>SESSION_USER()</strong>:SESSION_USER()和USER()具有相同的意义。</p>
<p><strong>SYSTEM_USER()</strong>:SYSTEM_USER()和USER()具有相同的意义</p>
<p><strong>USER()</strong>:返回当前MySQL用户名和机主名</p>
<p><strong>VERSION()</strong>:返回指示MySQL服务器版本的字符串。这个字符串使用utf8字符集。</p>
<h3 id="其他函数"><a href="#其他函数" class="headerlink" title="其他函数"></a>其他函数</h3><p><strong>DEFAULT(col_name)</strong>:返回一个表列的默认值。若该列没有默认值则会产生错误。</p>
<p><strong>GET_LOCK(str,timeout)</strong>:设法使用字符串str给定的名字得到一个锁，超时为timeout秒。若成功得到锁，则返回1，若操作超时则返回0 (例如,由于另一个客户端已提前封锁了这个名字),若发生错误则返回NULL(诸如缺乏记忆或线程mysqladmin kill被断开)。假如你有一个用GET_LOCK()得到的锁，当你执行RELEASE_LOCK()或你的连接断开(正常或非正常)时，这个锁就会解除</p>
<p><strong>INET_ATON(expr)</strong>:给出一个作为字符串的网络地址的点地址表示，返回一个代表该地址数值的整数。地址可以是4或8比特地址。</p>
<p><strong>INET_NTOA(expr)</strong>:给定一个数字网络地址(4或8比特),返回作为字符串的该地址的电地址表示</p>
<p><strong>IS_FREE_LOCK(str)</strong>:检查名为str的锁是否可以使用(换言之没有被封锁)。若锁可以使用，则返回1(没有人在用这个锁),若这个锁正在被使用，则返回0，出现错误则返回NULL(诸如不正确的参数)</p>
<p><strong>IS_USED_LOCK(str)</strong>:检查名为str的锁是否正在被使用(换言之,被封锁)。若被封锁，则返回使用该锁的客户端的连接标识符。否则返回NULL。</p>
<p><strong>MASTER_POS_WAIT(log_name ,log_pos [,timeout])</strong>:该函数对于控制主从同步很有用处。它会持续封锁，直到从设备阅读和应用主机记录中所有补充资料到指定的位置。返回值是其为到达指定位置而必须等待的记录事件的数目。若从设备SQL线程没有被启动、从设备主机信息尚未初始化、参数不正确或出现任何错误，则该函数返回NULL。若超时时间被超过，则返回-1。若在MASTER_POS_WAIT()等待期间，从设备SQL线程中止，则该函数返回NULL。若从设备由指定位置通过，则函数会立即返回结果。</p>
<p><strong>NAME_CONST(name,value)</strong>:返回给定值。当用来产生一个结果集合列时,NAME_CONST()促使该列使用给定名称。</p>
<p><strong>RELEASE_LOCK(str)</strong>:解开被GET_LOCK()获取的，用字符串str所命名的锁。若锁被解开，则返回1，若改线程尚未创建锁，则返回0(此时锁没有被解开),若命名的锁不存在，则返回NULL。若该锁从未被对GET_LOCK()的调用获取，或锁已经被提前解开，则该锁不存在。</p>
<p><strong>SLEEP(duration)</strong>:睡眠(暂停)时间为duration参数给定的秒数，然后返回0。若SLEEP()被中断,它会返回1。duration或许或包括一个给定的以微秒为单位的分数部分。</p>
<p><strong>UUID()</strong>:返回一个通用唯一标识符(UUID)，UUID被设计成一个在时间和空间上都独一无二的数字。2个对UUID()的调用应产生2个不同的值，即使这些调用的执行是在两个互不相连的单独电脑上进行。</p>
<p><strong>VALUES(col_name)</strong>:在一个<code>INSERT … ON DUPLICATE KEY UPDATE … </code>语句中，你可以在UPDATE子句中使用VALUES(col_name)函数，用来访问来自该语句的INSERT部分的列值。换言之，UPDATE子句中的VALUES(col_name)访问需要被插入的col_name的值,并不会发生重复键冲突。这个函数在多行插入中特别有用。VALUES()函数只在INSERT … UPDATE语句中有意义，而在其它情况下只会返回NULL</p>
<h3 id="聚合函数"><a href="#聚合函数" class="headerlink" title="聚合函数"></a>聚合函数</h3><p><strong>AVG([DISTINCT] expr)</strong>:返回expr的平均值。DISTINCT选项可用于返回expr的不同值的平均值。</p>
<p><strong>BIT_AND(expr)</strong>:返回expr中所有比特的bitwise AND。计算执行的精确度为64比特(BIGINT)。若找不到匹配的行，则这个函数返回18446744073709551615。(这是无符号BIGINT值，所有比特被设置为1)。</p>
<p><strong>BIT_OR(expr)</strong>:返回expr中所有比特的bitwise OR。计算执行的精确度为64比特(BIGINT)。若找不到匹配的行，则函数返回0。</p>
<p><strong>BIT_XOR(expr)</strong>:返回expr中所有比特的bitwise XOR。计算执行的精确度为64比特(BIGINT)。若找不到匹配的行，则函数返回0。</p>
<p><strong>COUNT(expr)</strong>:返回SELECT语句检索到的行中非NULL值的数目。若找不到匹配的行，则COUNT()返回0</p>
<p><strong>COUNT(DISTINCT expr ,[expr …])</strong>:返回不同的非NULL值数目。若找不到匹配的项，则COUNT(DISTINCT)返回0</p>
<p><strong>GROUP_CONCAT(expr)</strong>:该函数返回带有来自一个组的连接的非NULL值的字符串结果。其完整的语法如下所示：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">GROUP_CONCAT<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">DISTINCT</span><span class="token punctuation">]</span> expr <span class="token punctuation">[</span><span class="token punctuation">,</span>expr <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>

<span class="token punctuation">[</span><span class="token keyword">ORDER</span> <span class="token keyword">BY</span> &#123;unsigned_integer <span class="token operator">|</span> col_name <span class="token operator">|</span> expr &#125;

                 <span class="token punctuation">[</span><span class="token keyword">ASC</span> <span class="token operator">|</span> <span class="token keyword">DESC</span><span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token punctuation">,</span>col_name <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span>

             <span class="token punctuation">[</span>SEPARATOR str_val <span class="token punctuation">]</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>MIN([DISTINCT] expr), MAX([DISTINCT] expr)</strong>:返回expr的最小值和最大值。MIN()和MAX()的取值可以是一个字符串参数；在这些情况下，它们返回最小或最大字符串值。</p>
<p><strong>STD(expr),STDDEV(expr)</strong>:返回expr的总体标准偏差。这是标准SQL的延伸。这个函数的STDDEV()形式用来提供和Oracle的兼容性。可使用标准SQL函数STDDEV_POP()进行代替</p>
<p><strong>STDDEV_POP(expr)</strong>:返回expr的总体标准偏差(VAR_POP()的平方根)。你也可以使用STD()或STDDEV(),它们具有相同的意义，然而不是标准的SQL。若找不到匹配的行，则STDDEV_POP()返回NULL</p>
<p><strong>STDDEV_SAMP(expr)</strong>:返回expr的样本标准差(VAR_SAMP()的平方根)。若找不到匹配的行，则STDDEV_SAMP()返回NULL</p>
<p><strong>SUM([DISTINCT] expr)</strong>:返回expr的总数。若返回集合中无任何行，则SUM()返回NULL。DISTINCT关键词可用于MySQL5.1中，求得expr不同值的总和。若找不到匹配的行，则SUM()返回NULL</p>
<p><strong>VAR_POP(expr)</strong>:返回expr总体标准方差。它将行视为总体，而不是一个样本，所以它将行数作为分母。你也可以使用VARIANCE(),它具有相同的意义然而不是标准的SQL</p>
<p><strong>VAR_SAMP(expr)</strong>:返回expr的样本方差。更确切的说，分母的数字是行数减去1。若找不到匹配的行，则VAR_SAMP()返回NULL</p>
<p><strong>VARIANCE(expr)</strong>:返回expr的总体标准方差。这是标准SQL的延伸。可使用标准SQL函数VAR_POP()进行代替。若找不到匹配的项，则VARIANCE()返回NULL</p>
<h2 id="MySQL锁"><a href="#MySQL锁" class="headerlink" title="MySQL锁"></a>MySQL锁</h2><h3 id="表级锁和行级锁了解吗？有什么区别？"><a href="#表级锁和行级锁了解吗？有什么区别？" class="headerlink" title="表级锁和行级锁了解吗？有什么区别？"></a>表级锁和行级锁了解吗？有什么区别？</h3><p>MyISAM仅仅支持表级锁(table-level locking)，一锁就锁整张表，这在并发写的情况下性非常差。InnoDB不光支持表级锁(table-level locking)，还支持行级锁(row-level locking)，默认为行级锁。行级锁的粒度更小，仅对相关的记录上锁即可（对一行或者多行记录加锁），所以对于并发写入操作来说，InnoDB的性能更高。</p>
<p><strong>表级锁和行级锁对比</strong>：</p>
<ul>
<li><strong>表级锁</strong>:MySQL中锁定粒度最大的一种锁（全局锁除外），是针对非索引字段加的锁，对当前操作的整张表加锁，实现简单，资源消耗也比较少，加锁快，不会出现死锁。不过，触发锁冲突的概率最高，高并发下效率极低。表级锁和存储引擎无关，MyISAM和InnoDB引擎都支持表级锁。</li>
<li><strong>行级锁</strong>:MySQL中锁定粒度最小的一种锁，是针对索引字段加的锁，只针对当前操作的行记录进行加锁。行级锁能大大减少数据库操作的冲突。其加锁粒度最小，并发度高，但加锁的开销也最大，加锁慢，会出现死锁。行级锁和存储引擎有关，是在存储引擎层面实现的。</li>
</ul>
<h3 id="行级锁的使用有什么注意事项？"><a href="#行级锁的使用有什么注意事项？" class="headerlink" title="行级锁的使用有什么注意事项？"></a>行级锁的使用有什么注意事项？</h3><p>InnoDB的行锁是针对索引字段加的锁，表级锁是针对非索引字段加的锁。当我们执行<code>UPDATE</code>、<code>DELETE</code>语句时，如果<code>WHERE</code>条件中字段没有命中唯一索引或者索引失效的话，就会导致扫描全表对表中的所有行记录进行加锁。这个在我们日常工作开发中经常会遇到，一定要多多注意！不过，很多时候即使用了索引也有可能会走全表扫描，这是因为MySQL优化器的原因。</p>
<h3 id="InnoDB有哪几类行锁？"><a href="#InnoDB有哪几类行锁？" class="headerlink" title="InnoDB有哪几类行锁？"></a>InnoDB有哪几类行锁？</h3><p>InnoDB行锁是通过对索引数据页上的记录加锁实现的，MySQLInnoDB支持三种行锁定方式：</p>
<ul>
<li><strong>记录锁（RecordLock）</strong>：也被称为记录锁，属于单个行记录上的锁。</li>
<li><strong>间隙锁（GapLock）</strong>：锁定一个范围，不包括记录本身。</li>
<li><strong>临键锁（Next-KeyLock）</strong>：RecordLock+GapLock，锁定一个范围，包含记录本身，主要目的是为了解决幻读问题（MySQL事务部分提到过）。记录锁只能锁住已经存在的记录，为了避免插入新记录，需要依赖间隙锁。</li>
</ul>
<p>在InnoDB默认的隔离级别REPEATABLE-READ下，行锁默认使用的是Next-KeyLock。但是，如果操作的索引是唯一索引或主键，InnoDB会对Next-KeyLock进行优化，将其降级为RecordLock，即仅锁住索引本身，而不是范围。</p>
<blockquote>
<p>一些大厂面试中可能会问到Next-KeyLock的加锁范围，这里推荐一篇文章：<a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000040129107">MySQLnext-keylock加锁范围是什么？-程序员小航-2021</a>。</p>
</blockquote>
<h3 id="共享锁和排他锁呢？"><a href="#共享锁和排他锁呢？" class="headerlink" title="共享锁和排他锁呢？"></a>共享锁和排他锁呢？</h3><p>不论是表级锁还是行级锁，都存在共享锁（ShareLock，S锁）和排他锁（ExclusiveLock，X锁）这两类：</p>
<ul>
<li><strong>共享锁（S锁）</strong>：又称读锁，事务在读取记录的时候获取共享锁，允许多个事务同时获取（锁兼容）。</li>
<li><strong>排他锁（X锁）</strong>：又称写锁&#x2F;独占锁，事务在修改记录的时候获取排他锁，不允许多个事务同时获取。如果一个记录已经被加了排他锁，那其他事务不能再对这条事务加任何类型的锁（锁不兼容）。</li>
</ul>
<p>排他锁与任何的锁都不兼容，共享锁仅和共享锁兼容。</p>
<table>
<thead>
<tr>
<th align="left"></th>
<th align="left">S 锁</th>
<th align="left">X 锁</th>
</tr>
</thead>
<tbody><tr>
<td align="left">S 锁</td>
<td align="left">不冲突</td>
<td align="left">冲突</td>
</tr>
<tr>
<td align="left">X 锁</td>
<td align="left">冲突</td>
<td align="left">冲突</td>
</tr>
</tbody></table>
<p>由于MVCC的存在，对于一般的SELECT语句，InnoDB不会加任何锁。不过，你可以通过以下语句显式加共享锁或排他锁。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment"># 共享锁</span>
<span class="token keyword">SELECT</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token keyword">LOCK</span> <span class="token operator">IN</span> <span class="token keyword">SHARE</span> <span class="token keyword">MODE</span><span class="token punctuation">;</span>
<span class="token comment"># 排他锁</span>
<span class="token keyword">SELECT</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token keyword">FOR</span> <span class="token keyword">UPDATE</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="意向锁有什么作用？"><a href="#意向锁有什么作用？" class="headerlink" title="意向锁有什么作用？"></a>意向锁有什么作用？</h3><p>如果需要用到表锁的话，如何判断表中的记录没有行锁呢，一行一行遍历肯定是不行，性能太差。我们需要用到一个叫做意向锁的东东来快速判断是否可以对某个表使用表锁。</p>
<p>意向锁是表级锁，共有两种：</p>
<ul>
<li><strong>意向共享锁（IntentionSharedLock，IS锁）</strong>：事务有意向对表中的某些记录加共享锁（S锁），加共享锁前必须先取得该表的IS锁。</li>
<li><strong>意向排他锁（IntentionExclusiveLock，IX锁）</strong>：事务有意向对表中的某些记录加排他锁（X锁），加排他锁之前必须先取得该表的IX锁。</li>
</ul>
<p>意向锁是有数据引擎自己维护的，用户无法手动操作意向锁，在为数据行加共享&#x2F;排他锁之前，InooDB会先获取该数据行所在在数据表的对应意向锁。</p>
<p>意向锁之间是互相兼容的。</p>
<table>
<thead>
<tr>
<th></th>
<th>IS 锁</th>
<th>IX 锁</th>
</tr>
</thead>
<tbody><tr>
<td>IS 锁</td>
<td>兼容</td>
<td>兼容</td>
</tr>
<tr>
<td>IX 锁</td>
<td>兼容</td>
<td>兼容</td>
</tr>
</tbody></table>
<p>意向锁和共享锁和排它锁互斥（这里指的是表级别的共享锁和排他锁，意向锁不会与行级的共享锁和排他锁互斥）。</p>
<table>
<thead>
<tr>
<th></th>
<th>IS 锁</th>
<th>IX 锁</th>
</tr>
</thead>
<tbody><tr>
<td>S 锁</td>
<td>兼容</td>
<td>互斥</td>
</tr>
<tr>
<td>X 锁</td>
<td>互斥</td>
<td>互斥</td>
</tr>
</tbody></table>
<p>《MySQL技术内幕InnoDB存储引擎》这本书对应的描述应该是笔误了。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/mysql/image-20220511171419081.png" alt="img"></p>
<h3 id="当前读和快照读有什么区别？"><a href="#当前读和快照读有什么区别？" class="headerlink" title="当前读和快照读有什么区别？"></a>当前读和快照读有什么区别？</h3><p><strong>快照读</strong>（一致性非锁定读）就是单纯的SELECT语句，但不包括下面这两类SELECT语句：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>FORUPDATE
<span class="token keyword">SELECT</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>LOCKINSHAREMODE<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>快照即记录的历史版本，每行记录可能存在多个历史版本（多版本技术）。快照读的情况下，如果读取的记录正在执行UPDATE&#x2F;DELETE操作，读取操作不会因此去等待记录上X锁的释放，而是会去读取行的一个快照。只有在事务隔离级别RC(读取已提交)和RR（可重读）下，InnoDB才会使用一致性非锁定读：</p>
<ul>
<li>在RC级别下，对于快照数据，一致性非锁定读总是读取被锁定行的最新一份快照数据。</li>
<li>在RR级别下，对于快照数据，一致性非锁定读总是读取本事务开始时的行数据版本。</li>
</ul>
<p>快照读比较适合对于数据一致性要求不是特别高且追求极致性能的业务场景。<strong>当前读</strong>（一致性锁定读）就是给行记录加X锁或S锁。当前读的一些常见SQL语句类型如下：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment"># 对读的记录加一个X锁</span>
<span class="token keyword">SELECT</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token keyword">FOR</span> <span class="token keyword">UPDATE</span>
<span class="token comment"># 对读的记录加一个S锁</span>
<span class="token keyword">SELECT</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token keyword">LOCK</span> <span class="token operator">IN</span> <span class="token keyword">SHARE</span> <span class="token keyword">MODE</span>
<span class="token comment"># 对修改的记录加一个X锁</span>
<span class="token keyword">INSERT</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">UPDATE</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">DELETE</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="自增锁有了解吗？"><a href="#自增锁有了解吗？" class="headerlink" title="自增锁有了解吗？"></a>自增锁有了解吗？</h3><blockquote>
<p>不太重要的一个知识点，简单了解即可。</p>
</blockquote>
<p>关系型数据库设计表的时候，通常会有一列作为自增主键。InnoDB中的自增主键会涉及一种比较特殊的表级锁—<strong>自增锁（AUTO-INC Locks）</strong>。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token identifier"><span class="token punctuation">`</span>sequence_id<span class="token punctuation">`</span></span> <span class="token punctuation">(</span>
  <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token keyword">unsigned</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>stub<span class="token punctuation">`</span></span> <span class="token keyword">char</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token identifier"><span class="token punctuation">`</span>stub<span class="token punctuation">`</span></span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>stub<span class="token punctuation">`</span></span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8mb4<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>更准确点来说，不仅仅是自增主键，AUTO_INCREMENT的列都会涉及到自增锁，毕竟非主键也可以设置自增长。如果一个事务正在插入数据到有自增列的表时，会先获取自增锁，拿不到就可能会被阻塞住。这里的阻塞行为只是自增锁行为的其中一种，可以理解为自增锁就是一个接口，其具体的实现有多种。具体的配置项为innodb_autoinc_lock_mode（MySQL5.1.22引入），可以选择的值如下：</p>
<table>
<thead>
<tr>
<th align="center">innodb_autoinc_lock_mode</th>
<th align="center">介绍</th>
</tr>
</thead>
<tbody><tr>
<td align="center">0</td>
<td align="center">传统模式</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">连续模式（MySQL 8.0 之前默认）</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">交错模式(MySQL 8.0 之后默认)</td>
</tr>
</tbody></table>
<p>交错模式下，所有的“INSERT-LIKE”语句（所有的插入语句，包括：<code>INSERT</code>、<code>REPLACE</code>、<code>INSERT…SELECT</code>、<code>REPLACE…SELECT</code>、<code>LOADDATA</code>等）都不使用表级锁，使用的是轻量级互斥锁实现，多条插入语句可以并发执行，速度更快，扩展性也更好。不过，如果你的MySQL数据库有主从同步需求并且bin log存储格式为Statement的话，不要将InnoDB自增锁模式设置为交叉模式，不然会有数据不一致性问题。这是因为并发情况下插入语句的执行顺序就无法得到保障。</p>
<blockquote>
<p>如果MySQL采用的格式为Statement，那么MySQL的主从同步实际上同步的就是一条一条的SQL语句。</p>
</blockquote>
<p>最后，再推荐一篇文章：<a target="_blank" rel="noopener" href="https://draveness.me/whys-the-design-mysql-auto-increment/">为什么MySQL的自增主键不单调也不连续</a>。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><h4 id="数据库中的锁"><a href="#数据库中的锁" class="headerlink" title="数据库中的锁"></a>数据库中的锁</h4><p>表级锁包括：表锁、元数据锁、意向锁。<br>对于表锁而言，当存储引擎不支持行级锁时，使用表锁。SQL语句没有匹配到索引时，使用表锁。<br>对于元数据锁而言，对表做增删改查时，会加上MDL读锁。对表结构做变更时，会加上MDL写锁。<br>对于意向锁而言，对表中的行记录加锁时，会用到意向锁。<br>而对于行级锁而言，增删改查匹配到索引时，会使用行级锁</p>
<p>全局锁就是对整个数据库实例加锁。MySQL提供了一个加全局读锁的方法，命令是Flush tables with read lock(FTWRL)。当你需要让整个库处于只读状态的时候，可以使用这个命令，之后其他线程的以下语句会被阻塞：数据更新语句（数据的增删改）、数据定义语句（包括建表、修改表结构等）和更新类事务的提交语句。你可以理解为，全局锁基本上把数据所所有的变更语句都锁住了。全局锁的典型场景应用场景是全库逻辑备份，也就是把整个库每个表都select出来存起来。上面说到全局锁会锁住所有变更语句，但这只是对于MyISAM存储引擎而言的。对于Innodb而言，其可以利用MVCC实现数据的一致性视图，从而不需要锁整个库就可以实现全库的数据备份</p>
<p>表级锁可以分为：表锁、元数据锁、意向锁三种</p>
<ul>
<li><p>表锁，顾名思义就是对某个表加锁。那什么时候会使用表锁呢？一般情况是对应的存储引擎没有行级锁（例如：MyIASM），或者是对应的SQL语句没有匹配到索引。</p>
</li>
<li><p>元数据锁<br>元数据，指的是我们的表结构这些元数据。元数据锁（Metadata Lock）自然是执行DDL表结构变更语句时，我们对表加上的一个锁了。那什么时候会使用元数据锁这个表级锁呢？当我们对一个表做增删改查操作的时候，会加上MDL读锁；当我们要对表结构做变更时，就会加MDL写锁。</p>
</li>
<li><p>意向锁<br>意向锁，本质上就是空间换时间的产物，是为了提高行锁效率的一个东西。在InnoDB中，我们对某条记录进行锁定时，为了提高并发度，通常都只是锁定这一行记录，而不是锁定整个表。而当我们需要为整个表加X锁的时候，我们就需要遍历整个表的记录，如果每条记录都没有被加锁，才可以给整个表加X锁。而这个遍历过程就很费时间，这时候就有了意向锁的诞生。意向锁其实就是标记这个表有没有被锁，如果有某条记录被锁住了，那么就必须获取该表的意向锁。所以当我们需要判断这个表的记录有没有被加锁时，直接判断意向锁就可以了，减少了遍历的时间，提高了效率，是典型的用空间换时间的做法。那么什么时候会用到意向锁呢？很简单，就是在对表中的行记录加锁的时候，就会用到意向锁。</p>
</li>
</ul>
<p>那么什么时候会使用行级锁呢？<br>当增删改查匹配到索引时，Innodb会使用行级锁。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/v4GVad1wgW0H7tmjyQS0QA">MySQL啥时候用表锁，啥时候用行锁？</a></p>
</blockquote>
<p><strong>如果查询条件用了索引&#x2F;主键，那么<code>select ..... for update</code>就会进行行锁。</strong><br><strong>如果是普通字段(没有索引&#x2F;主键)，那么<code>select ..... for update</code>就会进行锁表。</strong></p>
<h4 id="如何尽可能避免死锁"><a href="#如何尽可能避免死锁" class="headerlink" title="如何尽可能避免死锁"></a>如何尽可能避免死锁</h4><ol>
<li>合理的设计索引，区分度高的列放到组合索引前面，使业务SQL尽可能通过索引定位更少的行，减少锁竞争。</li>
<li>调整业务逻辑SQL执行顺序，避免update&#x2F;delete长时间持有锁的SQL在事务前面。</li>
<li>避免大事务，尽量将大事务拆成多个小事务来处理，小事务发生锁冲突的几率也更小。</li>
<li>以固定的顺序访问表和行。比如两个更新数据的事务，事务A更新数据的顺序为1，2;事务B更新数据的顺序为2，1。这样更可能会造成死锁。</li>
<li>在并发比较高的系统中，不要显式加锁，特别是是在事务里显式加锁。如select…for update语句，如果是在事务里（运行了start transaction或设置了auto commit等于0）,那么就会锁定所查找到的记录。</li>
<li>尽量按主键&#x2F;索引去查找记录，范围查找增加了锁冲突的可能性，也不要利用数据库做一些额外额度计算工作。比如有的程序会用到“select…where…order by rand();”这样的语句，由于类似这样的语句用不到索引，因此将导致整个表的数据都被锁住。</li>
<li>优化SQL和表设计，减少同时占用太多资源的情况。比如说，减少连接的表，将复杂SQL分解为多个简单的SQL。</li>
</ol>
<h4 id="相关文章"><a href="#相关文章" class="headerlink" title="相关文章"></a>相关文章</h4><ul>
<li><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/7BuvuRFuelBTI2rn2If6cA">这六个MySQL死锁案例，能让你理解死锁的原因！</a></li>
<li><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/3EU5Yfd3O6i1CA-YvhqoVg">面试命中率90%的点：MySQL锁</a></li>
<li><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5lX30br8IBStcnk2J1PEXg">一文搞懂MySQL中各种锁，写的太好了</a></li>
</ul>
<h2 id="MySQL索引"><a href="#MySQL索引" class="headerlink" title="MySQL索引"></a>MySQL索引</h2><h3 id="索引介绍"><a href="#索引介绍" class="headerlink" title="索引介绍"></a>索引介绍</h3><p><strong>索引是一种用于快速查询和检索数据的数据结构，其本质可以看成是一种排序好的数据结构。</strong></p>
<p>索引的作用就相当于书的目录。打个比方:我们在查字典的时候，如果没有目录，那我们就只能一页一页的去找我们需要查的那个字，速度很慢。如果有目录了，我们只需要先去目录里查找字的位置，然后直接翻到那一页就行了。索引底层数据结构存在很多种类型，常见的索引结构有:B树，B+树和Hash、红黑树。在MySQL中，无论是Innodb还是MyIsam，都使用了B+树作为索引结构。</p>
<h3 id="索引的优缺点"><a href="#索引的优缺点" class="headerlink" title="索引的优缺点"></a>索引的优缺点</h3><p><strong>优点</strong>：</p>
<ul>
<li>使用索引可以大大加快数据的检索速度（大大减少检索的数据量）,这也是创建索引的最主要的原因。</li>
<li>通过创建唯一性索引，可以保证数据库表中每一行数据的唯一性。</li>
</ul>
<p><strong>缺点</strong>：</p>
<ul>
<li>创建索引和维护索引需要耗费许多时间。当对表中的数据进行增删改的时候，如果数据有索引，那么索引也需要动态的修改，会降低SQL执行效率。</li>
<li>索引需要使用物理文件存储，也会耗费一定空间。</li>
</ul>
<p>但是，<strong>使用索引一定能提高查询性能吗</strong>?大多数情况下，索引查询都是比全表扫描要快的。但是如果数据库的数据量不大，那么使用索引也不一定能够带来很大提升。</p>
<h3 id="索引的底层数据结构"><a href="#索引的底层数据结构" class="headerlink" title="索引的底层数据结构"></a>索引的底层数据结构</h3><h4 id="Hash表"><a href="#Hash表" class="headerlink" title="Hash表"></a>Hash表</h4><p>哈希表是键值对的集合，通过键(key)即可快速取出对应的值(value)，因此哈希表可以快速检索数据（接近O（1））。</p>
<p><strong>为何能够通过key快速取出value呢</strong>？原因在于哈希算法（也叫散列算法）。通过哈希算法，我们可以快速找到key对应的index，找到了index也就找到了对应的value。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">hash <span class="token operator">=</span> <span class="token function">hashfunc</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
index <span class="token operator">=</span> hash <span class="token operator">%</span> array_size<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><img src="https://oss.javaguide.cn/github/javaguide/database/mysql20210513092328171.png" alt="img"></p>
<p>但是哈希算法有个<strong>Hash冲突</strong>问题，也就是说多个不同的key最后得到的index相同。通常情况下，我们常用的解决办法是<strong>链地址法</strong>。链地址法就是将哈希冲突数据存放在链表中。就比如JDK1.8之前HashMap就是通过链地址法来解决哈希冲突的。不过，JDK1.8以后HashMap为了减少链表过长的时候搜索时间过长引入了红黑树。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/database/mysql20210513092224836.png" alt="img"></p>
<p>为了减少Hash冲突的发生，一个好的哈希函数应该“均匀地”将数据分布在整个可能的哈希值集合中。</p>
<p>既然哈希表这么快，<strong>为什么MySQL没有使用其作为索引的数据结构呢</strong>？主要是因为Hash索引不支持顺序和范围查询。假如我们要对表中的数据进行排序或者进行范围查询，那Hash索引可就不行了。并且，每次IO只能取一个。</p>
<p>试想一种情况:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token constant">SELECT</span> <span class="token operator">*</span> <span class="token constant">FROM</span> tb1 <span class="token constant">WHERE</span> id <span class="token operator">&lt;</span> <span class="token number">500</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>在这种范围查询中，优势非常大，直接遍历比500小的叶子节点就够了。而Hash索引是根据hash算法来定位的，难不成还要把1-499的数据，每个都进行一次hash计算来定位吗?这就是Hash最大的缺点了。</p>
<h4 id="B树-amp-B-树"><a href="#B树-amp-B-树" class="headerlink" title="B树&amp;B+树"></a>B树&amp;B+树</h4><p>B树也称B-树,全称为<strong>多路平衡查找树</strong>，B+树是B树的一种变体。B树和B+树中的B是Balanced（平衡）的意思。目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构。</p>
<p><strong>B树&amp;B+树两者有何异同呢？</strong></p>
<ul>
<li>B树的所有节点既存放键(key)也存放数据(data)，而B+树只有叶子节点存放key和data，其他内节点只存放key。</li>
<li>B树的叶子节点都是独立的;B+树的叶子节点有一条引用链指向与它相邻的叶子节点。</li>
<li>B树的检索的过程相当于对范围内的每个节点的关键字做二分查找，可能还没有到达叶子节点，检索就结束了。而B+树的检索效率就很稳定了，任何查找都是从根节点到叶子节点的过程，叶子节点的顺序检索很明显。</li>
</ul>
<p>在MySQL中，MyISAM引擎和InnoDB引擎都是使用B+Tree作为索引结构，但是，两者的实现方式不太一样。（下面的内容整理自《Java工程师修炼之道》）</p>
<blockquote>
<p>MyISAM引擎中，B+Tree叶节点的data域存放的是数据记录的地址。在索引检索的时候，首先按照B+Tree搜索算法搜索索引，如果指定的Key存在，则取出其data域的值，然后以data域的值为地址读取相应的数据记录。这被称为“<strong>非聚簇索引（非聚集索引）</strong>”。</p>
<p>InnoDB引擎中，其数据文件本身就是索引文件。相比MyISAM，索引文件和数据文件是分离的，其表数据文件本身就是按B+Tree组织的一个索引结构，树的叶节点data域保存了完整的数据记录。这个索引的key是数据表的主键，因此InnoDB表数据文件本身就是主索引。这被称为<strong>聚簇索引（聚集索引）</strong>，而其余的索引都作为<strong>辅助索引</strong>，辅助索引的data域存储相应记录主键的值而不是地址，这也是和MyISAM不同的地方。在根据主索引搜索时，直接找到key所在的节点即可取出数据；在根据辅助索引查找时，则需要先取出主键的值，再走一遍主索引。因此，在设计表的时候，不建议使用过长的字段作为主键，也不建议使用非单调的字段作为主键，这样会造成主索引频繁分裂。</p>
</blockquote>
<h3 id="索引类型总结"><a href="#索引类型总结" class="headerlink" title="索引类型总结"></a>索引类型总结</h3><p><strong>按照数据结构维度划分</strong>：</p>
<ul>
<li>BTree索引：MySQL里默认和最常用的索引类型。只有叶子节点存储value，非叶子节点只有指针和key。存储引擎MyISAM和InnoDB实现BTree索引都是使用B+Tree，但二者实现方式不一样（前面已经介绍了）。</li>
<li>哈希索引：类似键值对的形式，一次即可定位。</li>
<li>RTree索引：一般不会使用，仅支持geometry数据类型，优势在于范围查找，效率较低，通常使用搜索引擎如ElasticSearch代替。</li>
<li>全文索引：对文本的内容进行分词，进行搜索。目前只有<code>CHAR</code>、<code>VARCHAR</code>，<code>TEXT</code>列上可以创建全文索引。一般不会使用，效率较低，通常使用搜索引擎如ElasticSearch代替。</li>
</ul>
<p><strong>按照底层存储方式角度划分</strong>：</p>
<ul>
<li>聚簇索引（聚集索引）：索引结构和数据一起存放的索引，InnoDB中的主键索引就属于聚簇索引。</li>
<li>非聚簇索引（非聚集索引）：索引结构和数据分开存放的索引，二级索引(辅助索引)就属于非聚簇索引。MySQL的MyISAM引擎，不管主键还是非主键，使用的都是非聚簇索引。</li>
</ul>
<p><strong>按照应用维度划分</strong>：</p>
<ul>
<li>主键索引：加速查询+列值唯一（不可以有NULL）+表中只有一个。</li>
<li>普通索引：仅加速查询。</li>
<li>唯一索引：加速查询+列值唯一（可以有NULL）。</li>
<li>覆盖索引：一个索引包含（或者说覆盖）所有需要查询的字段的值。</li>
<li>联合索引：多列值组成一个索引，专门用于组合搜索，其效率大于索引合并。</li>
<li>全文索引：对文本的内容进行分词，进行搜索。目前只有<code>CHAR</code>、<code>VARCHAR</code>，<code>TEXT</code>列上可以创建全文索引。一般不会使用，效率较低，通常使用搜索引擎如ElasticSearch代替。</li>
</ul>
<p>MySQL8.x中实现的索引新特性：</p>
<ul>
<li>隐藏索引：也称为不可见索引，不会被优化器使用，但是仍然需要维护，通常会软删除和灰度发布的场景中使用。主键不能设置为隐藏（包括显式设置或隐式设置）。</li>
<li>降序索引：之前的版本就支持通过desc来指定索引为降序，但实际上创建的仍然是常规的升序索引。直到MySQL8.x版本才开始真正支持降序索引。另外，在MySQL8.x版本中，不再对GROUPBY语句进行隐式排序。</li>
<li>函数索引：从MySQL8.0.13版本开始支持在索引中使用函数或者表达式的值，也就是在索引中可以包含函数或者表达式。</li>
</ul>
<h3 id="主键索引-Primary-Key"><a href="#主键索引-Primary-Key" class="headerlink" title="主键索引(Primary Key)"></a>主键索引(Primary Key)</h3><p>数据表的主键列使用的就是主键索引。一张数据表有只能有一个主键，并且主键不能为null，不能重复。在MySQL的InnoDB的表中，当没有显示的指定表的主键时，InnoDB会自动先检查表中是否有唯一索引且不允许存在null值的字段，如果有，则选择该字段为默认的主键，否则InnoDB将会自动创建一个6Byte的自增主键。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/open-source-project/cluster-index.png" alt="img"></p>
<h3 id="二级索引"><a href="#二级索引" class="headerlink" title="二级索引"></a>二级索引</h3><p><strong>二级索引（SecondaryIndex）又称为辅助索引，是因为二级索引的叶子节点存储的数据是主键。也就是说，通过二级索引，可以定位主键的位置</strong>。</p>
<p>唯一索引，普通索引，前缀索引等索引属于二级索引。</p>
<ol>
<li><strong>唯一索引（UniqueKey）</strong>：唯一索引也是一种约束。唯一索引的属性列不能出现重复的数据，但是允许数据为NULL，一张表允许创建多个唯一索引。建立唯一索引的目的大部分时候都是为了该属性列的数据的唯一性，而不是为了查询效率。</li>
<li><strong>普通索引（Index）</strong>：普通索引的唯一作用就是为了快速查询数据，一张表允许创建多个普通索引，并允许数据重复和NULL。</li>
<li><strong>前缀索引（Prefix）</strong>：前缀索引只适用于字符串类型的数据。前缀索引是对文本的前几个字符创建索引，相比普通索引建立的数据更小，因为只取前几个字符。</li>
<li><strong>全文索引（FullText）</strong>：全文索引主要是为了检索大文本数据中的关键字的信息，是目前搜索引擎数据库使用的一种技术。Mysql5.6之前只有MYISAM引擎支持全文索引，5.6之后InnoDB也支持了全文索引。</li>
</ol>
<p>二级索引:</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/open-source-project/no-cluster-index.png" alt="img"></p>
<h3 id="聚簇索引与非聚簇索引"><a href="#聚簇索引与非聚簇索引" class="headerlink" title="聚簇索引与非聚簇索引"></a>聚簇索引与非聚簇索引</h3><h4 id="聚簇索引（聚集索引）"><a href="#聚簇索引（聚集索引）" class="headerlink" title="聚簇索引（聚集索引）"></a>聚簇索引（聚集索引）</h4><p><strong>聚簇索引介绍</strong></p>
<p><strong>聚簇索引（ClusteredIndex）即索引结构和数据一起存放的索引，并不是一种单独的索引类型。InnoDB中的主键索引就属于聚簇索引</strong>。</p>
<p>在MySQL中，InnoDB引擎的表的<code>.ibd</code>文件就包含了该表的索引和数据，对于InnoDB引擎表来说，该表的索引(B+树)的每个非叶子节点存储索引，叶子节点存储索引和索引对应的数据。</p>
<p><strong>聚簇索引的优缺点</strong></p>
<p><strong>优点</strong>：</p>
<ul>
<li><strong>查询速度非常快</strong>：聚簇索引的查询速度非常的快，因为整个B+树本身就是一颗多叉平衡树，叶子节点也都是有序的，定位到索引的节点，就相当于定位到了数据。相比于非聚簇索引，聚簇索引少了一次读取数据的IO操作。</li>
<li><strong>对排序查找和范围查找优化</strong>：聚簇索引对于主键的排序查找和范围查找速度非常快。</li>
</ul>
<p><strong>缺点</strong>：</p>
<ul>
<li><strong>依赖于有序的数据</strong>：因为B+树是多路平衡树，如果索引的数据不是有序的，那么就需要在插入时排序，如果数据是整型还好，否则类似于字符串或UUID这种又长又难比较的数据，插入或查找的速度肯定比较慢。</li>
<li><strong>更新代价大</strong>：如果对索引列的数据被修改时，那么对应的索引也将会被修改，而且聚簇索引的叶子节点还存放着数据，修改代价肯定是较大的，所以对于主键索引来说，主键一般都是不可被修改的。</li>
</ul>
<h4 id="非聚簇索引（非聚集索引）"><a href="#非聚簇索引（非聚集索引）" class="headerlink" title="非聚簇索引（非聚集索引）"></a>非聚簇索引（非聚集索引）</h4><p><strong>非聚簇索引介绍</strong></p>
<p>非聚簇索引(Non-ClusteredIndex)即索引结构和数据分开存放的索引，并不是一种单独的索引类型。二级索引(辅助索引)就属于非聚簇索引。MySQL的MyISAM引擎，不管主键还是非主键，使用的都是非聚簇索引。</p>
<p>非聚簇索引的叶子节点并不一定存放数据的指针，因为二级索引的叶子节点就存放的是主键，根据主键再回表查数据。</p>
<p><strong>非聚簇索引的优缺点</strong></p>
<p><strong>优点</strong>：</p>
<p>更新代价比聚簇索引要小。非聚簇索引的更新代价就没有聚簇索引那么大了，非聚簇索引的叶子节点是不存放数据的</p>
<p><strong>缺点</strong>：</p>
<ul>
<li><strong>依赖于有序的数据</strong>：跟聚簇索引一样，非聚簇索引也依赖于有序的数据</li>
<li><strong>可能会二次查询（回表）</strong>：这应该是非聚簇索引最大的缺点了。当查到索引对应的指针或主键后，可能还需要根据指针或主键再到数据文件或表中查询。</li>
</ul>
<p>这是MySQL的表的文件截图:</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/database/mysql20210420165311654.png" alt="img"></p>
<p>聚簇索引和非聚簇索引:</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/database/mysql20210420165326946.png" alt="img"></p>
<p><strong>非聚簇索引一定回表查询吗(覆盖索引)?非聚簇索引不一定回表查询</strong>。试想一种情况，用户准备使用SQL查询用户名，而用户名字段正好建立了索引。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> name <span class="token keyword">FROM</span> <span class="token keyword">table</span> <span class="token keyword">WHERE</span> name<span class="token operator">=</span><span class="token string">'guang19'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>那么这个索引的key本身就是name，查到对应的name直接返回就行了，无需回表查询。即使是MYISAM也是这样，虽然MYISAM的主键索引确实需要回表，因为它的主键索引的叶子节点存放的是指针。但是！<strong>如果SQL查的就是主键呢</strong>？</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> id <span class="token keyword">FROM</span> <span class="token keyword">table</span> <span class="token keyword">WHERE</span> id<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>主键索引本身的key就是主键，查到返回就行了。这种情况就称之为覆盖索引了。</p>
<h3 id="覆盖索引和联合索引"><a href="#覆盖索引和联合索引" class="headerlink" title="覆盖索引和联合索引"></a>覆盖索引和联合索引</h3><h4 id="覆盖索引"><a href="#覆盖索引" class="headerlink" title="覆盖索引"></a>覆盖索引</h4><p>如果一个索引包含（或者说覆盖）所有需要查询的字段的值，我们就称之为<strong>覆盖索引（CoveringIndex）</strong>。我们知道在InnoDB存储引擎中，如果不是主键索引，叶子节点存储的是主键+列值。最终还是要“回表”，也就是要通过主键再查找一次。这样就会比较慢覆盖索引就是把要查询出的列和索引是对应的，不做回表操作！覆盖索引即需要查询的字段正好是索引的字段，那么直接根据该索引，就可以查到数据了，而无需回表查询。</p>
<blockquote>
<p>如主键索引，如果一条SQL需要查询主键，那么正好根据主键索引就可以查到主键。再如普通索引，如果一条SQL需要查询name，name字段正好有索引，那么直接根据这个索引就可以查到数据，也无需回表。</p>
</blockquote>
<p><img src="https://oss.javaguide.cn/github/javaguide/database/mysql20210420165341868.png" alt="覆盖索引"></p>
<p>我们这里简单演示一下覆盖索引的效果。</p>
<p>1、创建一个名为cus_order的表，来实际测试一下这种排序方式。为了测试方便，cus_order这张表只有id、score、name这3个字段。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> cus_order <span class="token punctuation">(</span>
  id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">unsigned</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  score <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token operator">=</span><span class="token number">100000</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8mb4<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>2、定义一个简单的存储过程（PROCEDURE）来插入100w测试数据。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">DELIMITER</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">DEFINER</span><span class="token operator">=</span><span class="token identifier"><span class="token punctuation">`</span>root<span class="token punctuation">`</span></span><span class="token variable">@`%`</span> <span class="token keyword">PROCEDURE</span> <span class="token identifier"><span class="token punctuation">`</span>BatchinsertDataToCusOder<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token operator">IN</span> start_num <span class="token keyword">INT</span><span class="token punctuation">,</span><span class="token operator">IN</span> max_num <span class="token keyword">INT</span><span class="token punctuation">)</span>
<span class="token keyword">BEGIN</span>
      <span class="token keyword">DECLARE</span> i <span class="token keyword">INT</span> <span class="token keyword">default</span> start_num<span class="token punctuation">;</span>
      <span class="token keyword">WHILE</span> i <span class="token operator">&lt;</span> max_num <span class="token keyword">DO</span>
          <span class="token keyword">insert</span> <span class="token keyword">into</span> cus_order<span class="token punctuation">(</span>id<span class="token punctuation">,</span> score<span class="token punctuation">,</span> name<span class="token punctuation">)</span>
          <span class="token keyword">values</span> <span class="token punctuation">(</span>i<span class="token punctuation">,</span>RAND<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000000</span><span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span><span class="token string">'user'</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token keyword">SET</span> i <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
      <span class="token keyword">END</span> <span class="token keyword">WHILE</span><span class="token punctuation">;</span>
  <span class="token keyword">END</span><span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">DELIMITER</span> <span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>存储过程定义完成之后，我们执行存储过程即可！</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 插入100w+的随机数据</span>
<span class="token keyword">CALL</span> BatchinsertDataToCusOder<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1000000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>等待一会，100w的测试数据就插入完成了！</p>
<p>3、创建覆盖索引并使用EXPLAIN命令分析。</p>
<p>为了能够对这100w数据按照score进行排序，我们需要执行下面的SQL语句。</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 降序排序</span>
<span class="token keyword">SELECT</span> score<span class="token punctuation">,</span>name <span class="token keyword">FROM</span> cus_order <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> score <span class="token keyword">DESC</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>使用EXPLAIN命令分析这条SQL语句，通过Extra这一列的Using filesort，我们发现是没有用到覆盖索引的。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/mysql/not-using-covering-index-demo.png" alt="img"></p>
<p>不过这也是理所应当，毕竟我们现在还没有创建索引呢！</p>
<p>我们这里以score和name两个字段建立联合索引：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> cus_order <span class="token keyword">ADD</span> <span class="token keyword">INDEX</span> id_score_name<span class="token punctuation">(</span>score<span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>创建完成之后，再用EXPLAIN命令分析再次分析这条SQL语句。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/mysql/using-covering-index-demo.png" alt="img"></p>
<p>通过Extra这一列的Using index，说明这条SQL语句成功使用了覆盖索引。</p>
<blockquote>
<p>关于EXPLAIN命令的详细介绍请看：<a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-query-execution-plan.html">MySQL执行计划分析</a>这篇文章。</p>
</blockquote>
<h4 id="联合索引"><a href="#联合索引" class="headerlink" title="联合索引"></a>联合索引</h4><p>使用表中的多个字段创建索引，就是<strong>联合索引</strong>，也叫<strong>组合索引</strong>或<strong>复合索引</strong>。</p>
<p>以score和name两个字段建立联合索引：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> <span class="token identifier"><span class="token punctuation">`</span>cus_order<span class="token punctuation">`</span></span> <span class="token keyword">ADD</span> <span class="token keyword">INDEX</span> id_score_name<span class="token punctuation">(</span>score<span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="最左前缀匹配原则"><a href="#最左前缀匹配原则" class="headerlink" title="最左前缀匹配原则"></a>最左前缀匹配原则</h4><p>最左前缀匹配原则指的是，在使用联合索引时，MySQL会根据联合索引中的字段顺序，从左到右依次到查询条件中去匹配，如果查询条件中存在与联合索引中最左侧字段相匹配的字段，则就会使用该字段过滤一批数据，直至联合索引中全部字段匹配完成，或者在执行过程中遇到范围查询（如”&gt;”、”&lt;”）才会停止匹配。对于”&gt;&#x3D;”、”&lt;&#x3D;”、”BETWEEN”、”like”前缀匹配的范围查询，并不会停止匹配。所以，我们在使用联合索引时，可以将区分度高的字段放在最左边，这也可以过滤更多数据。</p>
<blockquote>
<p>相关阅读：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/8qemhRg5MgXs1So5YCv0fQ">联合索引的最左匹配原则,全网都在说的一个错误结论</a>。</p>
</blockquote>
<h3 id="索引下推"><a href="#索引下推" class="headerlink" title="索引下推"></a>索引下推</h3><p>索引下推（IndexConditionPushdown）是MySQL5.6版本中提供的一项索引优化功能，可以在非聚簇索引遍历过程中，对索引中包含的字段先做判断，过滤掉不符合条件的记录，减少回表次数。</p>
<h3 id="正确使用索引的一些建议"><a href="#正确使用索引的一些建议" class="headerlink" title="正确使用索引的一些建议"></a>正确使用索引的一些建议</h3><p><strong>选择合适的字段创建索引</strong></p>
<ul>
<li><strong>不为NULL的字段</strong>：索引字段的数据应该尽量不为NULL，因为对于数据为NULL的字段，数据库较难优化。如果字段频繁被查询，但又避免不了为NULL，建议使用0,1,true,false这样语义较为清晰的短值或短字符作为替代。</li>
<li><strong>被频繁查询的字段</strong>：我们创建索引的字段应该是查询操作非常频繁的字段。</li>
<li><strong>被作为条件查询的字段</strong>：被作为WHERE条件查询的字段，应该被考虑建立索引。</li>
<li><strong>频繁需要排序的字段</strong>：索引已经排序，这样查询可以利用索引的排序，加快排序查询时间。</li>
<li><strong>被经常频繁用于连接的字段</strong>：经常用于连接的字段可能是一些外键列，对于外键列并不一定要建立外键，只是说该列涉及到表与表的关系。对于频繁被连接查询的字段，可以考虑建立索引，提高多表连接查询的效率。</li>
</ul>
<p><strong>被频繁更新的字段应该慎重建立索引</strong></p>
<p>虽然索引能带来查询上的效率，但是维护索引的成本也是不小的。如果一个字段不被经常查询，反而被经常修改，那么就更不应该在这种字段上建立索引了。</p>
<p><strong>限制每张表上的索引数量</strong></p>
<p>索引并不是越多越好，建议单张表索引不超过5个！索引可以提高效率同样可以降低效率。索引可以增加查询效率，但同样也会降低插入和更新的效率，甚至有些情况下会降低查询效率。因为MySQL优化器在选择如何优化查询时，会根据统一信息，对每一个可以用到的索引来进行评估，以生成出一个最好的执行计划，如果同时有很多个索引都可以用于查询，就会增加MySQL优化器生成执行计划的时间，同样会降低查询性能。</p>
<p><strong>尽可能的考虑建立联合索引而不是单列索引</strong></p>
<p>因为索引是需要占用磁盘空间的，可以简单理解为每个索引都对应着一颗B+树。如果一个表的字段过多，索引过多，那么当这个表的数据达到一个体量后，索引占用的空间也是很多的，且修改索引时，耗费的时间也是较多的。如果是联合索引，多个字段在一个索引上，那么将会节约很大磁盘空间，且修改数据的操作效率也会提升。</p>
<p><strong>注意避免冗余索引</strong></p>
<p>冗余索引指的是索引的功能相同，能够命中索引(a,b)就肯定能命中索引(a)，那么索引(a)就是冗余索引。如（name,city）和（name）这两个索引就是冗余索引，能够命中前者的查询肯定是能够命中后者的在大多数情况下，都应该尽量扩展已有的索引而不是创建新索引。</p>
<p><strong>字符串类型的字段使用前缀索引代替普通索引</strong></p>
<p>前缀索引仅限于字符串类型，较普通索引会占用更小的空间，所以可以考虑使用前缀索引带替普通索引。</p>
<p><strong>避免索引失效</strong></p>
<p>索引失效也是慢查询的主要原因之一，常见的导致索引失效的情况有下面这些：</p>
<ul>
<li>使用SELECT*进行查询;</li>
<li>创建了组合索引，但查询条件未遵守最左匹配原则;</li>
<li>在索引列上进行计算、函数、类型转换等操作;</li>
<li>以%开头的LIKE查询比如like’%abc’;</li>
<li>查询条件中使用or，且or的前后条件中有一个列没有索引，涉及的索引都不会被使用到;</li>
<li>发生<a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/index-invalidation-caused-by-implicit-conversion.html">隐式转换</a>;</li>
<li>……</li>
</ul>
<p><strong>删除长期未使用的索引</strong></p>
<p>删除长期未使用的索引，不用的索引的存在会造成不必要的性能损耗。MySQL5.7可以通过查询sys库的schema_unused_indexes视图来查询哪些索引从未被使用。</p>
<p><strong>知道如何分析语句是否走索引查询</strong></p>
<p>我们可以使用EXPLAIN命令来分析SQL的执行计划，这样就知道语句是否命中索引了。执行计划是指一条SQL语句在经过MySQL查询优化器的优化会后，具体的执行方式。EXPLAIN并不会真的去执行相关的语句，而是通过查询优化器对语句进行分析，找出最优的查询方案，并显示对应的信息。EXPLAIN的输出格式如下：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">mysql<span class="token operator">></span> <span class="token keyword">EXPLAIN</span> <span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>score<span class="token punctuation">`</span></span><span class="token punctuation">,</span><span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span> <span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>cus_order<span class="token punctuation">`</span></span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token identifier"><span class="token punctuation">`</span>score<span class="token punctuation">`</span></span> <span class="token keyword">DESC</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+----------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> select_type <span class="token operator">|</span> <span class="token keyword">table</span>     <span class="token operator">|</span> partitions <span class="token operator">|</span> <span class="token keyword">type</span> <span class="token operator">|</span> possible_keys <span class="token operator">|</span> <span class="token keyword">key</span>  <span class="token operator">|</span> key_len <span class="token operator">|</span> ref  <span class="token operator">|</span> <span class="token keyword">rows</span>   <span class="token operator">|</span> filtered <span class="token operator">|</span> Extra          <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+----------------+</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> <span class="token keyword">SIMPLE</span>      <span class="token operator">|</span> cus_order <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span> <span class="token keyword">ALL</span>  <span class="token operator">|</span> <span class="token boolean">NULL</span>          <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span> <span class="token number">997572</span> <span class="token operator">|</span>   <span class="token number">100.00</span> <span class="token operator">|</span> <span class="token keyword">Using</span> filesort <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+----------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span><span class="token punctuation">,</span> <span class="token number">1</span> warning <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>各个字段的含义如下：</p>
<table>
<thead>
<tr>
<th align="center"><strong>列名</strong></th>
<th align="center"><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td align="center">id</td>
<td align="center">SELECT 查询的序列标识符</td>
</tr>
<tr>
<td align="center">select_type</td>
<td align="center">SELECT 关键字对应的查询类型</td>
</tr>
<tr>
<td align="center">table</td>
<td align="center">用到的表名</td>
</tr>
<tr>
<td align="center">partitions</td>
<td align="center">匹配的分区，对于未分区的表，值为 NULL</td>
</tr>
<tr>
<td align="center">type</td>
<td align="center">表的访问方法</td>
</tr>
<tr>
<td align="center">possible_keys</td>
<td align="center">可能用到的索引</td>
</tr>
<tr>
<td align="center">key</td>
<td align="center">实际用到的索引</td>
</tr>
<tr>
<td align="center">key_len</td>
<td align="center">所选索引的长度</td>
</tr>
<tr>
<td align="center">ref</td>
<td align="center">当使用索引等值查询时，与索引作比较的列或常量</td>
</tr>
<tr>
<td align="center">rows</td>
<td align="center">预计要读取的行数</td>
</tr>
<tr>
<td align="center">filtered</td>
<td align="center">按表条件过滤后，留存的记录数的百分比</td>
</tr>
<tr>
<td align="center">Extra</td>
<td align="center">附加信息</td>
</tr>
</tbody></table>
<blockquote>
<p>篇幅问题，我这里只是简单介绍了一下MySQL执行计划，详细介绍请看：<a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-query-execution-plan.html">MySQL执行计划分析</a>这篇文章<br><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-index.html">原文链接</a></p>
</blockquote>
<h3 id="总结-1"><a href="#总结-1" class="headerlink" title="总结"></a>总结</h3><p>索引什么情况下会失效</p>
<ol>
<li>使用!&#x3D;或者&lt;&gt;或者is null判断导致索引失效</li>
<li>类型不一致导致的索引失效，如主键为id，其他表的字段为这个表的主键,字段类型却不一样</li>
<li>函数导致的索引失效，如SUBSTRING(name,1,2) &#x3D; ‘wise’需要全表扫描</li>
<li>运算符导致的索引失效</li>
<li>OR引起的索引失效，因为如果OR判断的字段中有一个没有索引的话,引擎会放弃索引而产生全表扫描</li>
<li>模糊搜索LIKE导致的索引失效</li>
<li>NOT IN、NOT EXISTS导致索引失效</li>
<li>IS NULL不走索引，IS NOT NULL走索引</li>
<li>联合索引如果不遵循最左前缀原则，那么索引也将失效</li>
<li>索引列如果使用了隐式转换也会导致索引失效</li>
</ol>
<h3 id="相关文章-1"><a href="#相关文章-1" class="headerlink" title="相关文章"></a>相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/B1Dr_w3oeIsFTRtZCKS5Ow">如何防止MySQL索引失效</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wyotVRKbBJ7LGdqFSxZOFg">你设计索引的原则是什么？怎么避免索引失效？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/k7AXALxURez5nS34KZZIxA">超全的数据库建表&#x2F;SQL&#x2F;索引规范，建议贴在工位上！</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/hsvdbGXBcHPA0JOO7gkpjw">MySQL索引扫盲总结</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzkzODE3OTI0Ng==&mid=2247491020&idx=1&sn=f703a7a54cf9bccd8aed6dd384515945&source=41#wechat_redirect">为什么Mysql的常用引擎都默认使用B+树作为索引？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Ii9gF-TZZ84WghB8Xtwxig">为什么MySQL使用B+树，而不是B树或者Hash？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/opbgvTX_GDytR-MS3-6yEA">MySQL索引底层：B+树详解</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ZbkRWFT5rXIA9ZXzwTZTTA">为什么Mongodb索引用B树，而Mysql用B+树?</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/I6g_O2lq3iI6DdnxFGGsuA">腾讯面试官用「B+树」虐哭我了</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzIwMDgzMjc3NA==&mid=2247484720&idx=1&sn=7bd7774058e7886eeb3dedb38aa8657a&chksm=96f66759a181ee4f4c177a755c3ac6b6e97fef148bbf4afea8616f4edec33bf6d4f18cda9f69&scene=21#wechat_redirect">面试官:谈谈你对mysql索引的认识？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/RfA8r3IXECqmvvTJX4sz0w">面试的时候，如果你没掌握索引，绝对没戏！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/yxS4tpX_6fz9LBsh0UoHpw">什么是MySQ索引?</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/1RdEIq4EDYpIge_84dunEw">MySQL的索引实现原理</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/b5xc6mRzzqg5Bjd5scJ_ug">浅入浅出MySQL索引</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/F1EY0hY9WrzmeRp3C5C9Pw">别再一知半解啦，索引其实就这么回事</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/HUy5RPBsxhu7g5Sr-l3XlQ">MySQL索引凭什么让查询效率提高这么多？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/6N-Do7Av6y6VP_vK9CVK3A">索引为什么能提高查询性能….</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/YRbHVNeJNjvQcgC4PunW6w">常见索引类型</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/iS8V65my03EQtOQAkxfMag">主键索引就是聚集索引？MySQL索引类型大梳理</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/F0cEzIqecF4sWg7ZRmHKRQ">再聊MySQL聚簇索引</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5EymS2IyB7yyYsxX5UWrtw">MySQL主键自增也有坑?innodb_autoinc_lock_mode</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/9yeModGuGvDu5S0bW9sU6w">再有人问你MySQL索引原理，就把这篇文章甩给他！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/dKvPKUVDTM1OiP_qzNiFBg">为什么索引可以让查询变快？终于有人说清楚了！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/EyYBqfZcBWP60Y1gAADeOg">面试官问我索引为什么这快？我好像解释不清楚了</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/0_4Jq06LLaTEfjetLQP0iA">一文讲清，MySQL中的二级索引</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/_hdGKhB-A4ZOFZ42POmgjQ">明明加了唯一索引，为什么还是产生重复数据？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/4XfSv004Vy7hyvZsjlQXwQ">面试官提问：什么是前缀索引？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/IYRTE00_3bXD6y3YBW9P6Q">MySQL遵循最左前缀匹配原则！面试官：回去等通知吧</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/N95kMxVduiA6ZA3TSexAtA">MySQL索引15连问</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/QLQvMT2sPmmjVE_pXj5FIA">MySQL索引数据结构入门</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/mqi7MyF183FUmgy2FXi3Tw">前缀索引，在性能和空间中寻找平衡</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/bhlzlILDSpLkiRBfVyZj0w">聊聊mysql优化神器：前缀索引！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/SLIrSYnrNYkTyioE4syhFA">全文索引</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/L68fygoNmvmRuYSmOkHROA">MySQL模糊查询再也不用like+%了(全文索引)</a></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<h2 id="MySQL三大日志"><a href="#MySQL三大日志" class="headerlink" title="MySQL三大日志"></a>MySQL三大日志</h2><p>MySQL日志主要包括错误日志、查询日志、慢查询日志、事务日志、二进制日志几大类。其中，比较重要的还要属二进制日志bin log（归档日志）和事务日志redo log（重做日志）和undo log（回滚日志）。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/01.png" alt="img"></p>
<p>今天就来聊聊redo log（重做日志）、bin log（归档日志）、两阶段提交、undo log（回滚日志）。</p>
<h3 id="redo-log"><a href="#redo-log" class="headerlink" title="redo log"></a>redo log</h3><p>redo log（重做日志）是InnoDB存储引擎独有的，它让MySQL拥有了崩溃恢复能力。比如MySQL实例挂了或宕机了，重启时，InnoDB存储引擎会使用redo log恢复数据，保证数据的持久性与完整性。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/02.png" alt="img"></p>
<p>MySQL中数据是以页为单位，你查询一条记录，会从硬盘把一页的数据加载出来，加载出来的数据叫数据页，会放入到Buffer Pool中。后续的查询都是先从Buffer Pool中找，没有命中再去硬盘加载，减少硬盘IO开销，提升性能。更新表数据的时候，也是如此，发现Buffer Pool里存在要更新的数据，就直接在Buffer Pool里更新。然后会把“在某个数据页上做了什么修改”记录到重做日志缓存（redo log buffer）里，接着刷盘到redo log文件里。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/03.png" alt="img"></p>
<blockquote>
<p>图片笔误提示：第4步“清空redo log buffe刷盘到redo日志中”这句话中的buffe应该是buffer。</p>
</blockquote>
<p>理想情况，事务一提交就会进行刷盘操作，但实际上，刷盘的时机是根据策略来进行的。</p>
<blockquote>
<p>小贴士：每条redo记录由“表空间号+数据页号+偏移量+修改数据长度+具体修改的数据”组成</p>
</blockquote>
<h4 id="刷盘时机"><a href="#刷盘时机" class="headerlink" title="刷盘时机"></a>刷盘时机</h4><p>InnoDB存储引擎为redo log的刷盘策略提供了innodb_flush_log_at_trx_commit参数，它支持三种策略：</p>
<ul>
<li><strong>0</strong>：设置为0的时候，表示每次事务提交时不进行刷盘操作</li>
<li><strong>1</strong>：设置为1的时候，表示每次事务提交时都将进行刷盘操作（默认值）</li>
<li><strong>2</strong>：设置为2的时候，表示每次事务提交时都只把redo log buffer内容写入page cache</li>
</ul>
<p>innodb_flush_log_at_trx_commit参数默认为1，也就是说当事务提交时会调用fsync对redo log进行刷盘。另外，InnoDB存储引擎有一个后台线程，每隔1秒，就会把redo log buffer中的内容写到文件系统缓存（page cache），然后调用fsync刷盘。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/04.png" alt="img"></p>
<p>也就是说，一个没有提交事务的redo log记录，也可能会刷盘。</p>
<p><strong>为什么呢？</strong></p>
<p>因为在事务执行过程redo log记录是会写入redo log buffer中，这些redo log记录会被后台线程刷盘。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/05.png" alt="img"></p>
<p>除了后台线程每秒1次的轮询操作，还有一种情况，当redo log buffer占用的空间即将达到innodb_log_buffer_size一半的时候，后台线程会主动刷盘。下面是不同刷盘策略的流程图。</p>
<p><strong>innodb_flush_log_at_trx_commit&#x3D;0</strong></p>
<p><img src="https://oss.javaguide.cn/github/javaguide/06.png" alt="img"></p>
<p>为0时，如果MySQL挂了或宕机可能会有1秒数据的丢失。</p>
<p><strong>innodb_flush_log_at_trx_commit&#x3D;1</strong></p>
<p><img src="https://oss.javaguide.cn/github/javaguide/07.png" alt="img"></p>
<p>为1时，只要事务提交成功，redo log记录就一定在硬盘里，不会有任何数据丢失。如果事务执行期间MySQL挂了或宕机，这部分日志丢了，但是事务并没有提交，所以日志丢了也不会有损失。</p>
<p><strong>innodb_flush_log_at_trx_commit&#x3D;2</strong></p>
<p><img src="https://oss.javaguide.cn/github/javaguide/09.png" alt="img"></p>
<p>为2时，只要事务提交成功，redo log buffer中的内容只写入文件系统缓存（page cache）。如果仅仅只是MySQL挂了不会有任何数据丢失，但是宕机可能会有1秒数据的丢失。</p>
<h4 id="日志文件组"><a href="#日志文件组" class="headerlink" title="日志文件组"></a>日志文件组</h4><p>硬盘上存储的redo log日志文件不只一个，而是以一个<strong>日志文件组</strong>的形式出现的，每个的redo日志文件大小都是一样的。比如可以配置为一组4个文件，每个文件的大小是1GB，整个redo log日志文件组可以记录4G的内容。它采用的是环形数组形式，从头开始写，写到末尾又回到头循环写，如下图所示。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/10.png" alt="img"></p>
<p>在个<strong>日志文件组</strong>中还有两个重要的属性，分别是write pos、checkpoint</p>
<ul>
<li><strong>write pos</strong>是当前记录的位置，一边写一边后移</li>
<li><strong>checkpoint</strong>是当前要擦除的位置，也是往后推移</li>
</ul>
<p>每次刷盘redo log记录到<strong>日志文件组</strong>中，write pos位置就会后移更新。每次MySQL加载<strong>日志文件组</strong>恢复数据时，会清空加载过的redo log记录，并把checkpoint后移更新。write pos和checkpoint之间的还空着的部分可以用来写入新的redo log记录。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/11.png" alt="img"></p>
<p>如果write pos追上checkpoint，表示<strong>日志文件组</strong>满了，这时候不能再写入新的redo log记录，MySQL得停下来，清空一些记录，把checkpoint推进一下。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/12.png" alt="img"></p>
<h4 id="redo-log小结"><a href="#redo-log小结" class="headerlink" title="redo log小结"></a>redo log小结</h4><p>相信大家都知道redo log的作用和它的刷盘时机、存储形式。现在我们来思考一个问题：<strong>只要每次把修改后的数据页直接刷盘不就好了，还有redo log什么事</strong>？它们不都是刷盘么？差别在哪里？</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token number">1</span> <span class="token class-name">Byte</span> <span class="token operator">=</span> <span class="token number">8</span>bit
<span class="token number">1</span> <span class="token constant">KB</span> <span class="token operator">=</span> <span class="token number">1024</span> <span class="token class-name">Byte</span>
<span class="token number">1</span> <span class="token constant">MB</span> <span class="token operator">=</span> <span class="token number">1024</span> <span class="token constant">KB</span>
<span class="token number">1</span> <span class="token constant">GB</span> <span class="token operator">=</span> <span class="token number">1024</span> <span class="token constant">MB</span>
<span class="token number">1</span> <span class="token constant">TB</span> <span class="token operator">=</span> <span class="token number">1024</span> <span class="token constant">GB</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>实际上，数据页大小是16KB，刷盘比较耗时，可能就修改了数据页里的几Byte数据，有必要把完整的数据页刷盘吗？而且数据页刷盘是随机写，因为一个数据页对应的位置可能在硬盘文件的随机位置，所以性能是很差。如果是写redo log，一行记录可能就占几十Byte，只包含表空间号、数据页号、磁盘文件偏移量、更新值，再加上是顺序写，所以刷盘速度很快。所以用redo log形式记录修改内容，性能会远远超过刷数据页的方式，这也让数据库的并发能力更强。</p>
<blockquote>
<p>其实内存的数据页在一定时机也会刷盘，我们把这称为页合并，讲Buffer Pool的时候会对这块细说</p>
</blockquote>
<h3 id="bin-log"><a href="#bin-log" class="headerlink" title="bin log"></a>bin log</h3><p>redo log它是物理日志，记录内容是“在某个数据页上做了什么修改”，属于InnoDB存储引擎。而bin log是逻辑日志，记录内容是语句的原始逻辑，类似于“给ID&#x3D;2这一行的c字段加1”，属于MySQL Server层。不管用什么存储引擎，只要发生了表数据更新，都会产生bin log日志。那bin log到底是用来干嘛的？可以说MySQL数据库的<strong>数据备份、主备、主主、主从</strong>都离不开bin log，需要依靠bin log来同步数据，保证数据一致性。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/01-20220305234724956.png" alt="img"></p>
<p>bin log会记录所有涉及更新数据的逻辑操作，并且是顺序写。</p>
<h4 id="记录格式"><a href="#记录格式" class="headerlink" title="记录格式"></a>记录格式</h4><p>bin log日志有三种格式，可以通过bin log_format参数指定。</p>
<ul>
<li><strong>statement</strong></li>
<li><strong>row</strong></li>
<li><strong>mixed</strong></li>
</ul>
<p>指定statement，记录的内容是SQL语句原文，比如执行一条<code>update T set update_time=now( )where id=1</code>，记录的内容如下。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/02-20220305234738688.png" alt="img"></p>
<p>同步数据时，会执行记录的SQL语句，但是有个问题，update_time&#x3D;now()这里会获取当前系统时间，直接执行会导致与原库的数据不一致。为了解决这种问题，我们需要指定为row，记录的内容不再是简单的SQL语句了，还包含操作的具体数据，记录内容如下。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/03-20220305234742460.png" alt="img"></p>
<p>row格式记录的内容看不到详细信息，要通过mysql bin log工具解析出来。update_time&#x3D;now()变成了具体的时间update_time&#x3D;1627112756247，条件后面的@1、@2、@3都是该行数据第1个~3个字段的原始值（<strong>假设这张表只有3个字段</strong>）。这样就能保证同步数据的一致性，通常情况下都是指定为row，这样可以为数据库的恢复与同步带来更好的可靠性。但是这种格式，需要更大的容量来记录，比较占用空间，恢复与同步时会更消耗IO资源，影响执行速度。所以就有了一种折中的方案，指定为mixed，记录的内容是前两者的混合。MySQL会判断这条SQL语句是否可能引起数据不一致，如果是，就用row格式，否则就用statement格式。</p>
<h4 id="写入机制"><a href="#写入机制" class="headerlink" title="写入机制"></a>写入机制</h4><p>bin log的写入时机也非常简单，事务执行过程中，先把日志写到bin log cache，事务提交的时候，再把bin log cache写到bin log文件中。因为一个事务的bin log不能被拆开，无论这个事务多大，也要确保一次性写入，所以系统会给每个线程分配一个块内存作为bin log cache。我们可以通过bin log_cache_size参数控制单个线程bin log cache大小，如果存储内容超过了这个参数，就要暂存到磁盘（Swap）。bin log日志刷盘流程如下</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/04-20220305234747840.png" alt="img"></p>
<ul>
<li><strong>上图的write，是指把日志写入到文件系统的page cache，并没有把数据持久化到磁盘，所以速度比较快</strong></li>
<li><strong>上图的fsync，才是将数据持久化到磁盘的操作</strong></li>
</ul>
<p>write和fsync的时机，可以由参数sync_bin log控制，默认是0。为0的时候，表示每次提交事务都只write，由系统自行判断什么时候执行fsync。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/05-20220305234754405.png" alt="img"></p>
<p>虽然性能得到提升，但是机器宕机，page cache里面的bin log会丢失。为了安全起见，可以设置为1，表示每次提交事务都会执行fsync，就如同<strong>redo log日志刷盘流程</strong>一样。最后还有一种折中方式，可以设置为N(N&gt;1)，表示每次提交事务都write，但累积N个事务后才fsync。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/06-20220305234801592.png" alt="img"></p>
<p>在出现IO瓶颈的场景里，将sync_bin log设置成一个比较大的值，可以提升性能。同样的，如果机器宕机，会丢失最近N个事务的bin log日志。</p>
<h3 id="两阶段提交"><a href="#两阶段提交" class="headerlink" title="两阶段提交"></a>两阶段提交</h3><p>redo log（重做日志）让InnoDB存储引擎拥有了崩溃恢复能力。</p>
<p>bin log（归档日志）保证了MySQL集群架构的数据一致性。</p>
<p>虽然它们都属于持久化的保证，但是侧重点不同。在执行更新语句过程，会记录redo log与bin log两块日志，以基本的事务为单位，redo log在事务执行过程中可以不断写入，而bin log只有在提交事务时才写入，所以redo log与bin log的写入时机不一样。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/01-20220305234816065.png" alt="img"></p>
<p>回到正题，redo log与bin log两份日志之间的逻辑不一致，会出现什么问题？我们以update语句为例，假设id&#x3D;2的记录，字段c值是0，把字段c值更新成1，SQL语句为<code>update T set c=1 where id=2</code>。假设执行过程中写完redo log日志后，bin log日志写期间发生了异常，会出现什么情况呢？</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/02-20220305234828662.png" alt="img"></p>
<p>由于bin log没写完就异常，这时候bin log里面没有对应的修改记录。因此，之后用bin log日志恢复数据时，就会少这一次更新，恢复出来的这一行c值是0，而原库因为redo log日志恢复，这一行c值是1，最终数据不一致。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/03-20220305235104445.png" alt="img"></p>
<p>为了解决两份日志之间的逻辑一致问题，InnoDB存储引擎使用<strong>两阶段提交</strong>方案。原理很简单，将redo log的写入拆成了两个步骤prepare和commit，这就是<strong>两阶段提交</strong>。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/04-20220305234956774.png" alt="img"></p>
<p>使用<strong>两阶段提交</strong>后，写入bin log时发生异常也不会有影响，因为MySQL根据redo log日志恢复数据时，发现redo log还处于prepare阶段，并且没有对应bin log日志，就会回滚该事务。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/05-20220305234937243.png" alt="img"></p>
<p>再看一个场景，redo log设置commit阶段发生异常，那会不会回滚事务呢？</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/06-20220305234907651.png" alt="img"></p>
<p>并不会回滚事务，它会执行上图框住的逻辑，虽然redo log是处于prepare阶段，但是能通过事务id找到对应的bin log日志，所以MySQL认为是完整的，就会提交事务恢复数据。</p>
<h3 id="undo-log"><a href="#undo-log" class="headerlink" title="undo log"></a>undo log</h3><blockquote>
<p>我们知道如果想要保证事务的原子性，就需要在异常发生时，对已经执行的操作进行回滚，在MySQL中，恢复机制是通过回滚日志（undo log）实现的，所有事务进行的修改都会先记录到这个回滚日志中，然后再执行相关的操作。如果执行过程中遇到异常的话，我们直接利用回滚日志中的信息将数据回滚到修改之前的样子即可！并且，回滚日志会先于数据持久化到磁盘上。这样就保证了即使遇到数据库突然宕机等情况，当用户再次启动数据库的时候，数据库还能够通过查询回滚日志来回滚将之前未完成的事务。</p>
</blockquote>
<p>另外，MVCC的实现依赖于：<strong>隐藏字段、ReadView、undolog</strong>。在内部实现中，InnoDB通过数据行的DB_TRX_ID和ReadView来判断数据的可见性，如不可见，则通过数据行的DB_ROLL_PTR找到undo log中的历史版本。每个事务读到的数据版本可能是不一样的，在同一个事务中，用户只能看到该事务创建ReadView之前已经提交的修改和该事务本身做的修改</p>
<h3 id="总结三大日志"><a href="#总结三大日志" class="headerlink" title="总结三大日志"></a>总结三大日志</h3><blockquote>
<p>MySQLInnoDB引擎使用redo log(重做日志)保证事务的持久性，使用undolog(回滚日志)来保证事务的原子性。<br>MySQL数据库的数据备份、主备、主主、主从都离不开bin log，需要依靠bin log来同步数据，保证数据一致性。<br><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-logs.html">原文链接</a></p>
</blockquote>
<p>bin log,redo log,undo log</p>
<p><strong>bin log</strong><br>主要使用场景有两个，分别是主从复制和数据恢复<br>bin log记录了数据库表结构和表数据变更，比如update&#x2F;delete&#x2F;insert&#x2F;truncate&#x2F;create。它不会记录select（因为这没有对表没有进行变更）</p>
<p><strong>redo log</strong><br>包括两部分：一个是内存中的日志缓冲(redo log buffer)，另一个是磁盘上的日志文件(redo log file)。Mysql的基本存储结构是页(记录都存在页里边)，所以MySQL是先把这条记录所在的页找到，然后把该页加载到内存中，将对应记录进行修改。现在就可能存在一个问题：如果在内存中把数据改了，还没来得及落磁盘，而此时的数据库挂了怎么办？显然这次更改就丢了。所以引入redo log解决。MySQL每执行一条DML语句，先将记录写入redo log buffer（缓冲），后续某个时间点再一次性将多个操作记录写到redo log file（磁盘）。这种先写日志，再写磁盘的技术就是MySQL里经常说到的WAL(Write-Ahead Logging)技术。redo log也是写到磁盘，不过是顺序IO所以速度很快。</p>
<p><strong>bin log和 redo log区别</strong></p>
<ol>
<li>存储的内容<br>bin log记载的是update&#x2F;delete&#x2F;insert这样的SQL语句，而redo log记载的是物理修改的内容（xxxx页修改了xxx）。<br>所以在搜索资料的时候会有这样的说法：redo log记录的是数据的物理变化，bin log记录的是数据的逻辑变化</li>
<li>功能<br>redo log的作用是为持久化而生的。写完内存，如果数据库挂了，那我们可以通过redo log来恢复内存还没来得及刷到磁盘的数据，将redo log加载到内存里边，那内存就能恢复到挂掉之前的数据了。<br>bin log的作用是复制和恢复而生的。主从服务器需要保持数据的一致性，通过bin log来同步数据。如果整个数据库的数据都被删除了，bin log存储着所有的数据变更情况，那么可以通过bin log来对数据进行恢复。看到这里，你会想：”如果整个数据库的数据都被删除了，那我可以用redo log的记录来恢复吗？“不能,因为功能的不同，redo log存储的是物理数据的变更，如果我们内存的数据已经刷到了磁盘了，那redo log的数据就无效了。所以redo log不会存储着历史所有数据的变更，文件的内容会被覆盖的。</li>
<li>redo log是MySQL的InnoDB引擎所产生的。bin log无论MySQL用什么引擎，都会有的。</li>
</ol>
<p><strong>undo log</strong><br>主要有两个作用：回滚和多版本控制(MVCC)<br>在数据修改的时候，不仅记录了redo log，还记录undo log，如果因为某些原因导致事务失败或回滚了，可以用undo log进行回滚，undo log主要存储的也是逻辑日志，比如我们要insert一条数据了，那undo log会记录的一条对应的delete日志。我们要update一条记录时，它会记录一条对应相反的update记录。</p>
<h3 id="相关文章-2"><a href="#相关文章-2" class="headerlink" title="相关文章"></a>相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/0z6GmUp0Lb1hDUo0EyYiUg">MySQL undo log、bin log、redo log</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&mid=2247494134&idx=1&sn=241d91929e6a8b7b5f92db1528286645&source=41#wechat_redirect">必须了解的mysql三大日志-bin log、redo log和undo log</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/JLV8GEcF1z4D8oiDhFo6-g">MySQL日志(redo log 和 undo log)都是什么鬼？</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/d_CRZGbC6qpxdP4BYetFcA">MySQL不会丢失数据的秘密，就藏在它的7种日志里</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/IEG6O3xNv62moh5DYxPL2Q">Java代码中，如何监控Mysql的bin log？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/w4vPFHJkog2nbl68_0LOnw">手把手教你玩MySQL删库不跑路，直接把MySQL的bin log玩溜！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/FlhZsx9MBGY0ypXQMqe6wA">MySQL的bin log的三种格式这么好玩！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/0dBwL3nwcldbS7hv7O5rOQ">讲一讲MySQL数据备份杀手锏bin log</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/lvw89Ix73oTqc2juF4X5sg">3000帧动画图解MySQL为什么需要bin log、redo log和undo log</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/kqfMxkpYZ3ICkHfbv8ET1g">MySQL bin log的三个业务应用场景</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/299512401">MySQL中bin log与redo log的区别</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/PvQxdDMdC98mpwSb7pyXvw">听我讲完redo log、bin log原理，面试官老脸一红</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/JYuh2c3Tad8C_DP7LZO5bg">bin log恢复误删除的数据</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ayI190ZxhAoUracU3K_HTA">聊聊redo log是什么？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/cXQ3RFwi-4JgH_x-3HJQnA">MySQL为什么需要redo log？</a></td>
</tr>
</tbody></table>
<h2 id="InnoDB存储引擎对MVCC的实现"><a href="#InnoDB存储引擎对MVCC的实现" class="headerlink" title="InnoDB存储引擎对MVCC的实现"></a>InnoDB存储引擎对MVCC的实现</h2><h3 id="一致性非锁定读和锁定读"><a href="#一致性非锁定读和锁定读" class="headerlink" title="一致性非锁定读和锁定读"></a>一致性非锁定读和锁定读</h3><h4 id="一致性非锁定读"><a href="#一致性非锁定读" class="headerlink" title="一致性非锁定读"></a>一致性非锁定读</h4><p>对于<a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-consistent-read.html">一致性非锁定读（Consistent Nonlocking Reads）</a>的实现，通常做法是加一个版本号或者时间戳字段，在更新数据的同时版本号+1或者更新时间戳。查询时，将当前可见的版本号与对应记录的版本号进行比对，如果记录的版本小于可见版本，则表示该记录可见。在InnoDB存储引擎中，<a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-multi-versioning.html">多版本控制(multi versioning)</a>就是对非锁定读的实现。如果读取的行正在执行DELETE或UPDATE操作，这时读取操作不会去等待行上锁的释放。相反地，InnoDB存储引擎会去读取行的一个快照数据，对于这种读取历史数据的方式，我们叫它快照读(snapshot read)。在<code>Repeatable Read</code>和<code>Read Committed</code>两个隔离级别下，如果是执行普通的select语句（不包括select…lock in share mode,select…for update）则会使用一致性非锁定读（MVCC）。并且在Repeatable Read下MVCC实现了可重复读和防止部分幻读</p>
<h4 id="锁定读"><a href="#锁定读" class="headerlink" title="锁定读"></a>锁定读</h4><p>如果执行的是下列语句，就是<a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html">锁定读（Locking Reads）</a></p>
<ul>
<li>select … lock in share mode</li>
<li>select … for update</li>
<li>insert、update、delete操作</li>
</ul>
<p>在锁定读下，读取的是数据的最新版本，这种读也被称为当前读（current read）。锁定读会对读取到的记录加锁：</p>
<ul>
<li>select … lock in share mode：对记录加S锁，其它事务也可以加S锁，如果加x锁则会被阻塞</li>
<li>select … for update、insert、update、delete：对记录加X锁，且其它事务不能加任何锁</li>
</ul>
<p>在一致性非锁定读下，即使读取的记录已被其它事务加上X锁，这时记录也是可以被读取的，即读取的快照数据。上面说了，在Repeatable Read下MVCC防止了部分幻读，这边的“部分”是指在一致性非锁定读情况下，只能读取到第一次查询之前所插入的数据（根据Read View判断数据可见性，Read View在第一次查询时生成）。但是！如果是当前读，每次读取的都是最新数据，这时如果两次查询中间有其它事务插入数据，就会产生幻读。所以，InnoDB在实现Repeatable Read时，如果执行的是当前读，则会对读取的记录使用Next-key Lock，来防止其它事务在间隙间插入数据</p>
<h3 id="InnoDB对MVCC的实现"><a href="#InnoDB对MVCC的实现" class="headerlink" title="InnoDB对MVCC的实现"></a>InnoDB对MVCC的实现</h3><p>MVCC的实现依赖于：<strong>隐藏字段、Read View、undo log</strong>。在内部实现中，InnoDB通过数据行的DB_TRX_ID和Read View来判断数据的可见性，如不可见，则通过数据行的DB_ROLL_PTR找到undo log中的历史版本。每个事务读到的数据版本可能是不一样的，在同一个事务中，用户只能看到该事务创建Read View之前已经提交的修改和该事务本身做的修改</p>
<h4 id="隐藏字段"><a href="#隐藏字段" class="headerlink" title="隐藏字段"></a>隐藏字段</h4><p>在内部，InnoDB存储引擎为每行数据添加了三个<a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-multi-versioning.html">隐藏字段</a>：</p>
<ul>
<li>DB_TRX_ID（6字节）：表示最后一次插入或更新该行的事务id。此外，delete操作在内部被视为更新，只不过会在记录头Record header中的deleted_flag字段将其标记为已删除</li>
<li>DB_ROLL_PTR（7字节）回滚指针，指向该行的undo log。如果该行未被更新，则为空</li>
<li>DB_ROW_ID（6字节）：如果没有设置主键且该表没有唯一非空索引时，InnoDB会使用该id来生成聚簇索引</li>
</ul>
<h4 id="ReadView"><a href="#ReadView" class="headerlink" title="ReadView"></a>ReadView</h4><pre class="line-numbers language-c" data-language="c"><code class="language-c">class ReadView <span class="token punctuation">&#123;</span>
  <span class="token comment">/* ... */</span>
private<span class="token operator">:</span>
  <span class="token class-name">trx_id_t</span> m_low_limit_id<span class="token punctuation">;</span>      <span class="token comment">/* 大于等于这个ID的事务均不可见 */</span>

  <span class="token class-name">trx_id_t</span> m_up_limit_id<span class="token punctuation">;</span>       <span class="token comment">/* 小于这个ID的事务均可见 */</span>

  <span class="token class-name">trx_id_t</span> m_creator_trx_id<span class="token punctuation">;</span>    <span class="token comment">/* 创建该Read View的事务ID */</span>

  <span class="token class-name">trx_id_t</span> m_low_limit_no<span class="token punctuation">;</span>      <span class="token comment">/* 事务Number,小于该Number的Undo Logs均可以被Purge */</span>

  <span class="token class-name">ids_t</span> m_ids<span class="token punctuation">;</span>                  <span class="token comment">/* 创建Read View时的活跃事务列表 */</span>

  m_closed<span class="token punctuation">;</span>                     <span class="token comment">/* 标记Read View是否close */</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><a target="_blank" rel="noopener" href="https://github.com/facebook/mysql-8.0/blob/8.0/storage/innobase/include/read0types.h#L298">Read View</a>主要是用来做可见性判断，里面保存了“当前对本事务不可见的其他活跃事务”</p>
<p>主要有以下字段：</p>
<ul>
<li>m_low_limit_id：目前出现过的最大的事务ID+1，即下一个将被分配的事务ID。大于等于这个ID的数据版本均不可见</li>
<li>m_up_limit_id：活跃事务列表m_ids中最小的事务ID，如果m_ids为空，则m_up_limit_id为m_low_limit_id。小于这个ID的数据版本均可见</li>
<li>m_ids：Read View创建时其他未提交的活跃事务ID列表。创建Read View时，将当前未提交事务ID记录下来，后续即使它们修改了记录行的值，对于当前事务也是不可见的。m_ids不包括当前事务自己和已提交的事务（正在内存中）</li>
<li>m_creator_trx_id：创建该Read View的事务ID</li>
</ul>
<h4 id="undo-log-1"><a href="#undo-log-1" class="headerlink" title="undo-log"></a>undo-log</h4><p>undo log主要有两个作用：</p>
<ul>
<li>当事务回滚时用于将数据恢复到修改前的样子</li>
<li>另一个作用是MVCC，当读取记录时，若该记录被其他事务占用或当前版本对该事务不可见，则可以通过undo log读取之前的版本数据，以此实现非锁定读</li>
</ul>
<p>在InnoDB存储引擎中undo log分为两种：insert undo log和update undo log：</p>
<ol>
<li><p><strong>insert undo log</strong>：指在insert操作中产生的undo log。因为insert操作的记录只对事务本身可见，对其他事务不可见，故该undo log可以在事务提交后直接删除。不需要进行purge操作。insert时的数据初始状态：</p>
</li>
<li><p><strong>update undo log</strong>：update或delete操作中产生的undo log。该undo log可能需要提供MVCC机制，因此不能在事务提交时就进行删除。提交时放入undo log链表，等待purge线程进行最后的删除</p>
</li>
</ol>
<p>不同事务或者相同事务的对同一记录行的修改，会使该记录行的undo log成为一条链表，链首就是最新的记录，链尾就是最早的旧记录。</p>
<h4 id="数据可见性算法"><a href="#数据可见性算法" class="headerlink" title="数据可见性算法"></a>数据可见性算法</h4><p>在InnoDB存储引擎中，创建一个新事务后，执行每个select语句前，都会创建一个快照（ReadView），<strong>快照中保存了当前数据库系统中正处于活跃（没有commit）的事务的ID号</strong>。其实简单的说保存的是系统中当前不应该被本事务看到的其他事务ID列表（即m_ids）。当用户在这个事务中要读取某个记录行的时候，InnoDB会将该记录行的DB_TRX_ID与Read View中的一些变量及当前事务ID进行比较，判断是否满足可见性条件。</p>
<ol>
<li>如果记录DB_TRX_ID&lt; m_up_limit_id，那么表明最新修改该行的事务（DB_TRX_ID）在当前事务创建快照之前就提交了，所以该记录行的值对当前事务是可见的</li>
<li>如果DB_TRX_ID&gt;&#x3D;m_low_limit_id，那么表明最新修改该行的事务（DB_TRX_ID）在当前事务创建快照之后才修改该行，所以该记录行的值对当前事务不可见。跳到步骤5</li>
<li>m_ids为空，则表明在当前事务创建快照之前，修改该行的事务就已经提交了，所以该记录行的值对当前事务是可见的</li>
<li>如果m_up_limit_id&lt;&#x3D;DB_TRX_ID&lt; m_low_limit_id，表明最新修改该行的事务（DB_TRX_ID）在当前事务创建快照的时候可能处于“活动状态”或者“已提交状态”；所以就要对活跃事务列表m_ids进行查找（源码中是用的二分查找，因为是有序的）<ul>
<li>如果在活跃事务列表m_ids中能找到DB_TRX_ID，表明：①在当前事务创建快照前，该记录行的值被事务ID为DB_TRX_ID的事务修改了，但没有提交；或者②在当前事务创建快照后，该记录行的值被事务ID为DB_TRX_ID的事务修改了。这些情况下，这个记录行的值对当前事务都是不可见的。跳到步骤5</li>
<li>在活跃事务列表中找不到，则表明“id为trx_id的事务”在修改“该记录行的值”后，在“当前事务”创建快照前就已经提交了，所以记录行对当前事务可见</li>
</ul>
</li>
<li>在该记录行的DB_ROLL_PTR指针所指向的undolog取出快照记录，用快照记录的DB_TRX_ID跳到步骤1重新开始判断，直到找到满足的快照版本或返回空</li>
</ol>
<h3 id="RC和RR隔离级别下MVCC的差异"><a href="#RC和RR隔离级别下MVCC的差异" class="headerlink" title="RC和RR隔离级别下MVCC的差异"></a>RC和RR隔离级别下MVCC的差异</h3><p>在事务隔离级别RC和RR（InnoDB存储引擎的默认事务隔离级别）下，InnoDB存储引擎使用MVCC（非锁定一致性读），但它们生成Read View的时机却不同</p>
<ul>
<li>在RC隔离级别下的每次select查询前都生成一个Read View(m_ids列表)</li>
<li>在RR隔离级别下只在事务开始后第一次select数据前生成一个Read View（m_ids列表）</li>
</ul>
<h3 id="MVCC解决不可重复读问题"><a href="#MVCC解决不可重复读问题" class="headerlink" title="MVCC解决不可重复读问题"></a>MVCC解决不可重复读问题</h3><p>虽然RC和RR都通过MVCC来读取快照数据，但由于生成Read View时机不同，从而在RR级别下实现可重复读。</p>
<h4 id="在RC下Read-View生成情况"><a href="#在RC下Read-View生成情况" class="headerlink" title="在RC下Read View生成情况"></a>在RC下Read View生成情况</h4><p><strong>1.假设时间线来到T4，那么此时数据行id&#x3D;1的版本链为</strong>：</p>
<p><img src="https://javaguide.cn/assets/a3fd1ec6-8f37-42fa-b090-7446d488fd04.bf41f07c.png" alt="img"></p>
<p>由于RC级别下每次查询都会生成Read View，并且事务101、102并未提交，此时103事务生成的Read View中活跃的事务m_ids为：[101,102]，m_low_limit_id为：104，m_up_limit_id为：101，m_creator_trx_id为：103</p>
<ul>
<li>此时最新记录的DB_TRX_ID为101，m_up_limit_id&lt;&#x3D;101&lt; m_low_limit_id，所以要在m_ids列表中查找，发现DB_TRX_ID存在列表中，那么这个记录不可见</li>
<li>根据DB_ROLL_PTR找到undo log中的上一版本记录，上一条记录的DB_TRX_ID还是101，不可见</li>
<li>继续找上一条DB_TRX_ID为1，满足1&lt; m_up_limit_id，可见，所以事务103查询到数据为name&#x3D;菜花</li>
</ul>
<p><strong>2.时间线来到T6，数据的版本链为</strong>：</p>
<p><img src="https://javaguide.cn/assets/528559e9-dae8-4d14-b78d-a5b657c88391.2ff79120.png" alt="img"></p>
<p>因为在RC级别下，重新生成Read View，这时事务101已经提交，102并未提交，所以此时Read View中活跃的事务m_ids：[102]，m_low_limit_id为：104，m_up_limit_id为：102，m_creator_trx_id为：103</p>
<ul>
<li>此时最新记录的DB_TRX_ID为102，m_up_limit_id&lt;&#x3D;102&lt; m_low_limit_id，所以要在m_ids列表中查找，发现DB_TRX_ID存在列表中，那么这个记录不可见</li>
<li>根据DB_ROLL_PTR找到undo log中的上一版本记录，上一条记录的DB_TRX_ID为101，满足101&lt; m_up_limit_id，记录可见，所以在T6时间点查询到数据为name&#x3D;李四，与时间T4查询到的结果不一致，不可重复读！</li>
</ul>
<p><strong>3.时间线来到T9，数据的版本链为</strong>：</p>
<p><img src="https://javaguide.cn/assets/6f82703c-36a1-4458-90fe-d7f4edbac71a.c8de5ed7.png" alt="img"></p>
<p>重新生成ReadView，这时事务101和102都已经提交，所以m_ids为空，则m_up_limit_id&#x3D;m_low_limit_id&#x3D;104，最新版本事务ID为102，满足102&lt; m_low_limit_id，可见，查询结果为name&#x3D;赵六</p>
<blockquote>
<p>总结：在RC隔离级别下，事务在每次查询开始时都会生成并设置新的Read View，所以导致不可重复读</p>
</blockquote>
<h4 id="在RR下Read-View生成情况"><a href="#在RR下Read-View生成情况" class="headerlink" title="在RR下Read View生成情况"></a>在RR下Read View生成情况</h4><p>在可重复读级别下，只会在事务开始后第一次读取数据时生成一个Read View（m_ids列表）</p>
<p><strong>1.在T4情况下的版本链为</strong>：</p>
<p><img src="https://javaguide.cn/assets/0e906b95-c916-4f30-beda-9cb3e49746bf.3a363d10.png" alt="img"></p>
<p>在当前执行select语句时生成一个Read View，此时m_ids：[101,102]，m_low_limit_id为：104，m_up_limit_id为：101，m_creator_trx_id为：103</p>
<p>此时和RC级别下一样：</p>
<ul>
<li>最新记录的DB_TRX_ID为101，m_up_limit_id&lt;&#x3D;101&lt; m_low_limit_id，所以要在m_ids列表中查找，发现DB_TRX_ID存在列表中，那么这个记录不可见</li>
<li>根据DB_ROLL_PTR找到undolog中的上一版本记录，上一条记录的DB_TRX_ID还是101，不可见</li>
<li>继续找上一条DB_TRX_ID为1，满足1&lt; m_up_limit_id，可见，所以事务103查询到数据为name&#x3D;菜花</li>
</ul>
<p><strong>2.时间点T6情况下</strong>：</p>
<p><img src="https://javaguide.cn/assets/79ed6142-7664-4e0b-9023-cf546586aa39.9c5cd303.png" alt="img"></p>
<p>在RR级别下只会生成一次Read View，所以此时依然沿用m_ids：[101,102]，m_low_limit_id为：104，m_up_limit_id为：101，m_creator_trx_id为：103</p>
<ul>
<li>最新记录的DB_TRX_ID为102，m_up_limit_id&lt;&#x3D;102&lt; m_low_limit_id，所以要在m_ids列表中查找，发现DB_TRX_ID存在列表中，那么这个记录不可见</li>
<li>根据DB_ROLL_PTR找到undo log中的上一版本记录，上一条记录的DB_TRX_ID为101，不可见</li>
<li>继续根据DB_ROLL_PTR找到undo log中的上一版本记录，上一条记录的DB_TRX_ID还是101，不可见</li>
<li>继续找上一条DB_TRX_ID为1，满足1&lt; m_up_limit_id，可见，所以事务103查询到数据为name&#x3D;菜花</li>
</ul>
<p><strong>3.时间点T9情况下</strong>：</p>
<p><img src="https://javaguide.cn/assets/cbbedbc5-0e3c-4711-aafd-7f3d68a4ed4e.7b4a86c0.png" alt="img"></p>
<p>此时情况跟T6完全一样，由于已经生成了Read View，此时依然沿用m_ids：[101,102]，所以查询结果依然是name&#x3D;菜花</p>
<h3 id="MVCC➕Next-key-Lock防止幻读"><a href="#MVCC➕Next-key-Lock防止幻读" class="headerlink" title="MVCC➕Next-key-Lock防止幻读"></a>MVCC➕Next-key-Lock防止幻读</h3><p>InnoDB存储引擎在RR级别下通过MVCC和Next-keyLock来解决幻读问题：</p>
<p><strong>1、执行普通select，此时会以MVCC快照读的方式读取数据</strong></p>
<p>在快照读的情况下，RR隔离级别只会在事务开启后的第一次查询生成Read View，并使用至事务提交。所以在生成Read View之后其它事务所做的更新、插入记录版本对当前事务并不可见，实现了可重复读和防止快照读下的“幻读”</p>
<p><strong>2、执行select…for update&#x2F;lock in share mode、insert、update、delete等当前读</strong></p>
<p>在当前读下，读取的都是最新的数据，如果其它事务有插入新的记录，并且刚好在当前事务查询范围内，就会产生幻读！InnoDB使用<a target="_blank" rel="noopener" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html#innodb-next-key-locks">Next-keyLock</a>来防止这种情况。当执行当前读时，会锁定读取到的记录的同时，锁定它们的间隙，防止其它事务在查询范围内插入数据。只要我不让你插入，就不会发生幻读</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/innodb-implementation-of-mvcc.html">原文链接</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wE43jx9svrHg5U16sq8CYw">面试中的MVCC到底是什么意思？</a></p>
</blockquote>
<h2 id="SQL语句在MySQL中的执行过程"><a href="#SQL语句在MySQL中的执行过程" class="headerlink" title="SQL语句在MySQL中的执行过程"></a>SQL语句在MySQL中的执行过程</h2><h3 id="MySQL基础架构分析"><a href="#MySQL基础架构分析" class="headerlink" title="MySQL基础架构分析"></a>MySQL基础架构分析</h3><h4 id="MySQL基本架构概览"><a href="#MySQL基本架构概览" class="headerlink" title="MySQL基本架构概览"></a>MySQL基本架构概览</h4><p>下图是MySQL的一个简要架构图，从下图你可以很清晰的看到用户的SQL语句在MySQL内部是如何执行的。</p>
<p>先简单介绍一下下图涉及的一些组件的基本作用帮助大家理解这幅图，在下节中会详细介绍到这些组件的作用。</p>
<ul>
<li><strong>连接器</strong>:身份认证和权限相关(登录MySQL的时候)。</li>
<li><strong>查询缓存</strong>:执行查询语句的时候，会先查询缓存（MySQL8.0版本后移除，因为这个功能不太实用）。</li>
<li><strong>分析器</strong>:没有命中缓存的话，SQL语句就会经过分析器，分析器说白了就是要先看你的SQL语句要干嘛，再检查你的SQL语句语法是否正确。</li>
<li><strong>优化器</strong>:按照MySQL认为最优的方案去执行。</li>
<li><strong>执行器</strong>:执行语句，然后从存储引擎返回数据。</li>
</ul>
<p><img src="https://oss.javaguide.cn/javaguide/13526879-3037b144ed09eb88.png" alt="img"></p>
<p>简单来说MySQL主要分为Server层和存储引擎层：</p>
<ul>
<li><strong>Server层</strong>：主要包括连接器、查询缓存、分析器、优化器、执行器等，所有跨存储引擎的功能都在这一层实现，比如存储过程、触发器、视图，函数等，还有一个通用的日志模块bin log日志模块。</li>
<li><strong>存储引擎</strong>：主要负责数据的存储和读取，采用可以替换的插件式架构，支持InnoDB、MyISAM、Memory等多个存储引擎，其中InnoDB引擎有自有的日志模块redo log模块。现在最常用的存储引擎是InnoDB，它从MySQL5.5版本开始就被当做默认存储引擎了。</li>
</ul>
<h4 id="Server层基本组件介绍"><a href="#Server层基本组件介绍" class="headerlink" title="Server层基本组件介绍"></a>Server层基本组件介绍</h4><h5 id="连接器"><a href="#连接器" class="headerlink" title="连接器"></a>连接器</h5><p>连接器主要和身份认证和权限相关的功能相关，就好比一个级别很高的门卫一样。</p>
<p>主要负责用户登录数据库，进行用户的身份认证，包括校验账户密码，权限等操作，如果用户账户密码已通过，连接器会到权限表中查询该用户的所有权限，之后在这个连接里的权限逻辑判断都是会依赖此时读取到的权限数据，也就是说，后续只要这个连接不断开，即使管理员修改了该用户的权限，该用户也是不受影响的。</p>
<h5 id="查询缓存-MySQL8-0版本后移除"><a href="#查询缓存-MySQL8-0版本后移除" class="headerlink" title="查询缓存(MySQL8.0版本后移除)"></a>查询缓存(MySQL8.0版本后移除)</h5><p>查询缓存主要用来缓存我们所执行的SELECT语句以及该语句的结果集。</p>
<p>连接建立后，执行查询语句的时候，会先查询缓存，MySQL会先校验这个SQL是否执行过，以Key-Value的形式缓存在内存中，Key是查询预计，Value是结果集。如果缓存key被命中，就会直接返回给客户端，如果没有命中，就会执行后续的操作，完成后也会把结果缓存起来，方便下一次调用。当然在真正执行缓存查询的时候还是会校验用户的权限，是否有该表的查询条件。</p>
<p>MySQL查询不建议使用缓存，因为查询缓存失效在实际业务场景中可能会非常频繁，假如你对一个表更新的话，这个表上的所有的查询缓存都会被清空。对于不经常更新的数据来说，使用缓存还是可以的。</p>
<p>所以，一般在大多数情况下我们都是不推荐去使用查询缓存的。</p>
<p>MySQL8.0版本后删除了缓存的功能，官方也是认为该功能在实际的应用场景比较少，所以干脆直接删掉了。</p>
<h5 id="分析器"><a href="#分析器" class="headerlink" title="分析器"></a>分析器</h5><p>MySQL没有命中缓存，那么就会进入分析器，分析器主要是用来分析SQL语句是来干嘛的，分析器也会分为几步：</p>
<p><strong>第一步，词法分析</strong>，一条SQL语句有多个字符串组成，首先要提取关键字，比如select，提出查询的表，提出字段名，提出查询条件等等。做完这些操作后，就会进入第二步。</p>
<p><strong>第二步，语法分析</strong>，主要就是判断你输入的SQL是否正确，是否符合MySQL的语法。</p>
<p>完成这2步之后，MySQL就准备开始执行了，但是如何执行，怎么执行是最好的结果呢？这个时候就需要优化器上场了。</p>
<h5 id="优化器"><a href="#优化器" class="headerlink" title="优化器"></a>优化器</h5><p>优化器的作用就是它认为的最优的执行方案去执行（有时候可能也不是最优，这篇文章涉及对这部分知识的深入讲解），比如多个索引的时候该如何选择索引，多表查询的时候如何选择关联顺序等。</p>
<p>可以说，经过了优化器之后可以说这个语句具体该如何执行就已经定下来。</p>
<h5 id="执行器"><a href="#执行器" class="headerlink" title="执行器"></a>执行器</h5><p>当选择了执行方案后，MySQL就准备开始执行了，首先执行前会校验该用户有没有权限，如果没有权限，就会返回错误信息，如果有权限，就会去调用引擎的接口，返回接口执行的结果。</p>
<h3 id="二语句分析"><a href="#二语句分析" class="headerlink" title="二语句分析"></a>二语句分析</h3><h4 id="查询语句"><a href="#查询语句" class="headerlink" title="查询语句"></a>查询语句</h4><p>说了以上这么多，那么究竟一条SQL语句是如何执行的呢？其实我们的SQL可以分为两种，一种是查询，一种是更新（增加，修改，删除）。我们先分析下查询语句，语句如下：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tb_student A <span class="token keyword">where</span> A<span class="token punctuation">.</span>age<span class="token operator">=</span><span class="token string">'18'</span> <span class="token operator">and</span> A<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">'张三'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>结合上面的说明，我们分析下这个语句的执行流程：</p>
<ul>
<li><p>先检查该语句是否有权限，如果没有权限，直接返回错误信息，如果有权限，在MySQL8.0版本以前，会先查询缓存，以这条SQL语句为key在内存中查询是否有结果，如果有直接缓存，如果没有，执行下一步。</p>
</li>
<li><p>通过分析器进行词法分析，提取SQL语句的关键元素，比如提取上面这个语句是查询select，提取需要查询的表名为tb_student，需要查询所有的列，查询条件是这个表的id&#x3D;’1’。然后判断这个SQL语句是否有语法错误，比如关键词是否正确等等，如果检查没问题就执行下一步。</p>
</li>
<li><p>接下来就是优化器进行确定执行方案，上面的SQL语句，可以有两种执行方案：</p>
</li>
</ul>
<pre class="line-numbers language-none"><code class="language-none">a. 先查询学生表中姓名为“张三”的学生，然后判断是否年龄是18。
b. 先找出学生中年龄18岁的学生，然后再查询姓名为“张三”的学生。<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>那么优化器根据自己的优化算法进行选择执行效率最好的一个方案（优化器认为，有时候不一定最好）。那么确认了执行计划后就准备开始执行了。</p>
<ul>
<li>进行权限校验，如果没有权限就会返回错误信息，如果有权限就会调用数据库引擎接口，返回引擎的执行结果。</li>
</ul>
<h4 id="更新语句"><a href="#更新语句" class="headerlink" title="更新语句"></a>更新语句</h4><p>以上就是一条查询SQL的执行流程，那么接下来我们看看一条更新语句如何执行的呢？SQL语句如下：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">update</span> tb_student A <span class="token keyword">set</span> A<span class="token punctuation">.</span>age<span class="token operator">=</span><span class="token string">'19'</span> <span class="token keyword">where</span> A<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">'张三'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>我们来给张三修改下年龄，在实际数据库肯定不会设置年龄这个字段的，不然要被技术负责人打的。其实这条语句也基本上会沿着上一个查询的流程走，只不过执行更新的时候肯定要记录日志啦，这就会引入日志模块了，MySQL自带的日志模块是<strong>bin log（归档日志）</strong>，所有的存储引擎都可以使用，我们常用的InnoDB引擎还自带了一个日志模块<strong>redo log（重做日志）</strong>，我们就以InnoDB模式下来探讨这个语句的执行流程。流程如下：</p>
<ul>
<li>先查询到张三这一条数据，如果有缓存，也是会用到缓存。</li>
<li>然后拿到查询的语句，把age改为19，然后调用引擎API接口，写入这一行数据，InnoDB引擎把数据保存在内存中，同时记录redo log，此时redo log进入prepare状态，然后告诉执行器，执行完成了，随时可以提交。</li>
<li>执行器收到通知后记录bin log，然后调用引擎接口，提交redo log为提交状态。</li>
<li>更新完成。</li>
</ul>
<p><strong>这里肯定有同学会问，为什么要用两个日志模块，用一个日志模块不行吗</strong>？</p>
<p>这是因为最开始MySQL并没有InnoDB引擎（InnoDB引擎是其他公司以插件形式插入MySQL的），MySQL自带的引擎是MyISAM，但是我们知道redo log是InnoDB引擎特有的，其他存储引擎都没有，这就导致会没有crash-safe的能力(crash-safe的能力即使数据库发生异常重启，之前提交的记录都不会丢失)，bin log日志只能用来归档。</p>
<p>并不是说只用一个日志模块不可以，只是InnoDB引擎就是通过redo log来支持事务的。那么，又会有同学问，我用两个日志模块，但是不要这么复杂行不行，为什么redo log要引入prepare预提交状态？这里我们用反证法来说明下为什么要这么做？</p>
<ul>
<li><strong>先写redo log直接提交，然后写bin log</strong>，假设写完redo log后，机器挂了，bin log日志没有被写入，那么机器重启后，这台机器会通过redo log恢复数据，但是这个时候bin log并没有记录该数据，后续进行机器备份的时候，就会丢失这一条数据，同时主从同步也会丢失这一条数据。</li>
<li><strong>先写binlog，然后写redo log</strong>，假设写完了bin log，机器异常重启了，由于没有redo log，本机是无法恢复这一条记录的，但是bin log又有记录，那么和上面同样的道理，就会产生数据不一致的情况。</li>
</ul>
<p>如果采用redo log两阶段提交的方式就不一样了，写完bin log后，然后再提交redo log就会防止出现上述的问题，从而保证了数据的一致性。那么问题来了，有没有一个极端的情况呢？假设redo log处于预提交状态，bin log也已经写完了，这个时候发生了异常重启会怎么样呢？这个就要依赖于MySQL的处理机制了，MySQL的处理过程如下：</p>
<ul>
<li>判断redo log是否完整，如果判断是完整的，就立即提交。</li>
<li>如果redo log只是预提交但不是commit状态，这个时候就会去判断bin log是否完整，如果完整就提交redo log,不完整就回滚事务。</li>
</ul>
<p>这样就解决了数据一致性的问题。</p>
<h3 id="总结-2"><a href="#总结-2" class="headerlink" title="总结"></a>总结</h3><ul>
<li>MySQL主要分为Server层和引擎层，Server层主要包括连接器、查询缓存、分析器、优化器、执行器，同时还有一个日志模块（bin log），这个日志模块所有执行引擎都可以共用，redo log只有InnoDB有。</li>
<li>引擎层是插件式的，目前主要包括，MyISAM,InnoDB,Memory等。</li>
<li>查询语句的执行流程如下：权限校验（如果命中缓存）—&gt;查询缓存—&gt;分析器—&gt;优化器—&gt;权限校验—&gt;执行器—&gt;引擎</li>
<li>更新语句执行流程如下：分析器—-&gt;权限校验—-&gt;执行器—&gt;引擎—redo log(prepare状态)—&gt;bin log—&gt;redo log(commit状态)</li>
</ul>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/how-sql-executed-in-mysql.html">原文链接</a></p>
</blockquote>
<h3 id="sql执行顺序"><a href="#sql执行顺序" class="headerlink" title="sql执行顺序"></a>sql执行顺序</h3><pre class="line-numbers language-none"><code class="language-none">from、on、join、where、group by、(avg,sum)、having、select、distinct、union、order by、limit<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="相关文章-3"><a href="#相关文章-3" class="headerlink" title="相关文章"></a>相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzkzODE3OTI0Ng==&mid=2247490897&idx=1&sn=e5eaa58d426255c2c1401578d5606138&source=41#wechat_redirect">面试官：你来讲讲一条查询语句的具体执行过程</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/dLJet2geb-dQ9hLE0oL6Qw">一文读懂MySQL查询语句的执行过程</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/DS4-ObUFeGsU2l6FvF0Pcw">MySQL的执行过程及执行顺序</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/k1Zjr6ucFZzI-w6wC2Hz3Q">图解SQL执行顺序，通俗易懂！</a></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<h2 id="MySQL查询缓存详解"><a href="#MySQL查询缓存详解" class="headerlink" title="MySQL查询缓存详解"></a>MySQL查询缓存详解</h2><p>缓存是一个有效且实用的系统性能优化的手段，不论是操作系统还是各种软件和网站或多或少都用到了缓存。然而，有经验的DBA都建议生产环境中把MySQL自带的QueryCache（查询缓存）给关掉。而且，从MySQL5.7.20开始，就已经默认弃用查询缓存了。在MySQL8.0及之后，更是直接删除了查询缓存的功能。这又是为什么呢？查询缓存真就这么鸡肋么?带着如下几个问题，我们正式进入本文。</p>
<ul>
<li>MySQL查询缓存是什么？适用范围？</li>
<li>MySQL缓存规则是什么？</li>
<li>MySQL缓存的优缺点是什么？</li>
<li>MySQL缓存对性能有什么影响？</li>
</ul>
<h3 id="MySQL查询缓存介绍"><a href="#MySQL查询缓存介绍" class="headerlink" title="MySQL查询缓存介绍"></a>MySQL查询缓存介绍</h3><p>MySQL体系架构如下图所示：</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/mysql/mysql-architecture.png" alt="img"></p>
<p>为了提高完全相同的查询语句的响应速度，MySQL Server会对查询语句进行Hash计算得到一个Hash值。MySQL Server不会对SQL做任何处理，SQL必须完全一致Hash值才会一样。得到Hash值之后，通过该Hash值到查询缓存中匹配该查询的结果。</p>
<ul>
<li>如果匹配（命中），则将查询的结果集直接返回给客户端，不必再解析、执行查询。</li>
<li>如果没有匹配（未命中），则将Hash值和结果集保存在查询缓存中，以便以后使用。</li>
</ul>
<p>也就是说，<strong>一个查询语句（select）到了MySQLServer之后，会先到查询缓存看看，如果曾经执行过的话，就直接返回结果集给客户端</strong>。</p>
<p><img src="https://oss.javaguide.cn/javaguide/13526879-3037b144ed09eb88.png" alt="img"></p>
<h3 id="MySQL查询缓存管理和配置"><a href="#MySQL查询缓存管理和配置" class="headerlink" title="MySQL查询缓存管理和配置"></a>MySQL查询缓存管理和配置</h3><p>通过<code>showvariableslike&#39;%query_cache%&#39;</code>命令可以查看查询缓存相关的信息。</p>
<p>8.0版本之前的话，打印的信息可能是下面这样的：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mysql<span class="token operator">></span> show variables like <span class="token string">'%query_cache%'</span><span class="token punctuation">;</span>
+------------------------------+---------+
<span class="token operator">|</span> Variable_name                <span class="token operator">|</span> Value   <span class="token operator">|</span>
+------------------------------+---------+
<span class="token operator">|</span> have_query_cache             <span class="token operator">|</span> YES     <span class="token operator">|</span>
<span class="token operator">|</span> query_cache_limit            <span class="token operator">|</span> <span class="token number">1048576</span> <span class="token operator">|</span>
<span class="token operator">|</span> query_cache_min_res_unit     <span class="token operator">|</span> <span class="token number">4096</span>    <span class="token operator">|</span>
<span class="token operator">|</span> query_cache_size             <span class="token operator">|</span> <span class="token number">599040</span>  <span class="token operator">|</span>
<span class="token operator">|</span> query_cache_type             <span class="token operator">|</span> ON      <span class="token operator">|</span>
<span class="token operator">|</span> query_cache_wlock_invalidate <span class="token operator">|</span> OFF     <span class="token operator">|</span>
+------------------------------+---------+
<span class="token number">6</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.02</span> sec<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>8.0以及之后版本之后，打印的信息是下面这样的：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mysql<span class="token operator">></span> show variables like <span class="token string">'%query_cache%'</span><span class="token punctuation">;</span>
+------------------+-------+
<span class="token operator">|</span> Variable_name    <span class="token operator">|</span> Value <span class="token operator">|</span>
+------------------+-------+
<span class="token operator">|</span> have_query_cache <span class="token operator">|</span> NO    <span class="token operator">|</span>
+------------------+-------+
<span class="token number">1</span> row <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.01</span> sec<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>我们这里对8.0版本之前show variables like ‘%query_cache%’;命令打印出来的信息进行解释。</p>
<ul>
<li><strong>have_query_cache</strong>:该MySQL Server是否支持查询缓存，如果是YES表示支持，否则则是不支持。</li>
<li><strong>query_cache_limit</strong>:MySQL查询缓存的最大查询结果，查询结果大于该值时不会被缓存。</li>
<li><strong>query_cache_min_res_unit</strong>:查询缓存分配的最小块的大小(字节)。当查询进行的时候，MySQL把查询结果保存在查询缓存中，但如果要保存的结果比较大，超过query_cache_min_res_unit的值，这时候MySQL将一边检索结果，一边进行保存结果，也就是说，有可能在一次查询中，MySQL要进行多次内存分配的操作。适当的调节query_cache_min_res_unit可以优化内存。</li>
<li><strong>query_cache_size</strong>:为缓存查询结果分配的内存的数量，单位是字节，且数值必须是1024的整数倍。默认值是0，即禁用查询缓存。</li>
<li><strong>query_cache_type</strong>:设置查询缓存类型，默认为ON。设置GLOBAL值可以设置后面的所有客户端连接的类型。客户端可以设置SESSION值以影响他们自己对查询缓存的使用。</li>
<li><strong>query_cache_wlock_invalidate</strong>:如果某个表被锁住，是否返回缓存中的数据，默认关闭，也是建议的。</li>
</ul>
<p>query_cache_type可能的值(修改query_cache_type需要重启MySQLServer)：</p>
<ul>
<li>0或OFF：关闭查询功能。</li>
<li>1或ON：开启查询缓存功能，但不缓存SelectSQL_NO_CACHE开头的查询。</li>
<li>2或DEMAND：开启查询缓存功能，但仅缓存SelectSQL_CACHE开头的查询。</li>
</ul>
<p><strong>建议</strong>：</p>
<ul>
<li>query_cache_size不建议设置的过大。过大的空间不但挤占实例其他内存结构的空间，而且会增加在缓存中搜索的开销。建议根据实例规格，初始值设置为10MB到100MB之间的值，而后根据运行使用情况调整。</li>
<li>建议通过调整query_cache_size的值来开启、关闭查询缓存，因为修改query_cache_type参数需要重启MySQL Server生效。</li>
</ul>
<p>8.0版本之前，my.cnf加入以下配置，重启MySQL开启查询缓存</p>
<pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token key attr-name">query_cache_type</span><span class="token punctuation">=</span><span class="token value attr-value">1</span>
<span class="token key attr-name">query_cache_size</span><span class="token punctuation">=</span><span class="token value attr-value">600000</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>或者，MySQL执行以下命令也可以开启查询缓存</p>
<pre class="line-numbers language-properties" data-language="properties"><code class="language-properties"><span class="token key attr-name">set</span> <span class="token value attr-value">global query_cache_type=1;</span>
<span class="token key attr-name">set</span> <span class="token value attr-value">global query_cache_size=600000;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>手动清理缓存可以使用下面三个SQL：</p>
<ul>
<li>flush query cache;：清理查询缓存内存碎片。</li>
<li>reset query cache;：从查询缓存中移除所有查询。</li>
<li>flush tables；关闭所有打开的表，同时该操作会清空查询缓存中的内容。</li>
</ul>
<h3 id="MySQL缓存机制"><a href="#MySQL缓存机制" class="headerlink" title="MySQL缓存机制"></a>MySQL缓存机制</h3><p><strong>缓存规则</strong></p>
<ul>
<li>查询缓存会将查询语句和结果集保存到内存（一般是key-value的形式，key是查询语句，value是查询的结果集），下次再查直接从内存中取。</li>
<li>缓存的结果是通过sessions共享的，所以一个client查询的缓存结果，另一个client也可以使用。</li>
<li>SQL必须完全一致才会导致查询缓存命中（大小写、空格、使用的数据库、协议版本、字符集等必须一致）。检查查询缓存时，MySQL Server不会对SQL做任何处理，它精确的使用客户端传来的查询。</li>
<li>不缓存查询中的子查询结果集，仅缓存查询最终结果集。</li>
<li>不确定的函数将永远不会被缓存,比如now()、curdate()、last_insert_id()、rand()等。</li>
<li>不缓存产生告警（Warnings）的查询。</li>
<li>太大的结果集不会被缓存(&lt; query_cache_limit)。</li>
<li>如果查询中包含任何用户自定义函数、存储函数、用户变量、临时表、MySQL库中的系统表，其查询结果也不会被缓存。</li>
<li>缓存建立之后，MySQL的查询缓存系统会跟踪查询中涉及的每张表，如果这些表（数据或结构）发生变化，那么和这张表相关的所有缓存数据都将失效。</li>
<li>MySQL缓存在分库分表环境下是不起作用的。</li>
<li>不缓存使用SQL_NO_CACHE的查询。</li>
<li>……</li>
</ul>
<p>查询缓存SELECT选项示例：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 会缓存</span>
<span class="token keyword">SELECT</span> SQL_CACHE id<span class="token punctuation">,</span> name <span class="token keyword">FROM</span> customer<span class="token punctuation">;</span>
<span class="token comment">-- 不会缓存</span>
<span class="token keyword">SELECT</span> SQL_NO_CACHE id<span class="token punctuation">,</span> name <span class="token keyword">FROM</span> customer<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>缓存机制中的内存管理</strong></p>
<p>查询缓存是完全存储在内存中的，所以在配置和使用它之前，我们需要先了解它是如何使用内存的。MySQL查询缓存使用内存池技术，自己管理内存释放和分配，而不是通过操作系统。内存池使用的基本单位是变长的block,用来存储类型、大小、数据等信息。一个结果集的缓存通过链表把这些block串起来。block最短长度为<code>query_cache_min_res_unit</code>。当服务器启动的时候，会初始化缓存需要的内存，是一个完整的空闲块。当查询结果需要缓存的时候，先从空闲块中申请一个数据块为参数<code>query_cache_min_res_unit</code>配置的空间，即使缓存数据很小，申请数据块也是这个，因为查询开始返回结果的时候就分配空间，此时无法预知结果多大。分配内存块需要先锁住空间块，所以操作很慢，MySQL会尽量避免这个操作，选择尽可能小的内存块，如果不够，继续申请，如果存储完时有空余则释放多余的。但是如果并发的操作，余下的需要回收的空间很小，小于<code>query_cache_min_res_unit</code>，不能再次被使用，就会产生碎片。</p>
<h3 id="MySQL查询缓存的优缺点"><a href="#MySQL查询缓存的优缺点" class="headerlink" title="MySQL查询缓存的优缺点"></a>MySQL查询缓存的优缺点</h3><p><strong>优点</strong></p>
<ul>
<li>查询缓存的查询，发生在MySQL接收到客户端的查询请求、查询权限验证之后和查询SQL解析之前。也就是说，当MySQL接收到客户端的查询SQL之后，仅仅只需要对其进行相应的权限验证之后，就会通过查询缓存来查找结果，甚至都不需要经过Optimizer模块进行执行计划的分析优化，更不需要发生任何存储引擎的交互。</li>
<li>由于查询缓存是基于内存的，直接从内存中返回相应的查询结果，因此减少了大量的磁盘I&#x2F;O和CPU计算，导致效率非常高。</li>
</ul>
<p><strong>缺点</strong></p>
<ul>
<li>MySQL会对每条接收到的SELECT类型的查询进行Hash计算，然后查找这个查询的缓存结果是否存在。虽然Hash计算和查找的效率已经足够高了，一条查询语句所带来的开销可以忽略，但一旦涉及到高并发，有成千上万条查询语句时，hash计算和查找所带来的开销就必须重视了。</li>
<li>查询缓存的失效问题。如果表的变更比较频繁，则会造成查询缓存的失效率非常高。表的变更不仅仅指表中的数据发生变化，还包括表结构或者索引的任何变化。</li>
<li>查询语句不同，但查询结果相同的查询都会被缓存，这样便会造成内存资源的过度消耗。查询语句的字符大小写、空格或者注释的不同，查询缓存都会认为是不同的查询（因为他们的Hash值会不同）。</li>
<li>相关系统变量设置不合理会造成大量的内存碎片，这样便会导致查询缓存频繁清理内存。</li>
</ul>
<h3 id="MySQL查询缓存对性能的影响"><a href="#MySQL查询缓存对性能的影响" class="headerlink" title="MySQL查询缓存对性能的影响"></a>MySQL查询缓存对性能的影响</h3><p>在MySQL Server中打开查询缓存对数据库的读和写都会带来额外的消耗:</p>
<ul>
<li>读查询开始之前必须检查是否命中缓存。</li>
<li>如果读查询可以缓存，那么执行完查询操作后，会查询结果和查询语句写入缓存。</li>
<li>当向某个表写入数据的时候，必须将这个表所有的缓存设置为失效，如果缓存空间很大，则消耗也会很大，可能使系统僵死一段时间，因为这个操作是靠全局锁操作来保护的。</li>
<li>对InnoDB表，当修改一个表时，设置了缓存失效，但是多版本特性会暂时将这修改对其他事务屏蔽，在这个事务提交之前，所有查询都无法使用缓存，直到这个事务被提交，所以长时间的事务，会大大降低查询缓存的命中。</li>
</ul>
<h3 id="总结-3"><a href="#总结-3" class="headerlink" title="总结"></a>总结</h3><p>MySQL中的查询缓存虽然能够提升数据库的查询性能，但是查询同时也带来了额外的开销，每次查询后都要做一次缓存操作，失效后还要销毁。查询缓存是一个适用较少情况的缓存机制。如果你的应用对数据库的更新很少，那么查询缓存将会作用显著。比较典型的如博客系统，一般博客更新相对较慢，数据表相对稳定不变，这时候查询缓存的作用会比较明显。简单总结一下查询缓存的适用场景：</p>
<ul>
<li>表数据修改不频繁、数据较静态。</li>
<li>查询（Select）重复度高。</li>
<li>查询结果集小于1MB。</li>
</ul>
<p>对于一个更新频繁的系统来说，查询缓存缓存的作用是很微小的，在某些情况下开启查询缓存会带来性能的下降。简单总结一下查询缓存不适用的场景：</p>
<ul>
<li>表中的数据、表结构或者索引变动频繁</li>
<li>重复的查询很少</li>
<li>查询的结果集很大</li>
</ul>
<p>《高性能MySQL》这样写到：</p>
<blockquote>
<p>根据我们的经验，在高并发压力环境中查询缓存会导致系统性能的下降，甚至僵死。如果你一定要使用查询缓存，那么不要设置太大内存，而且只有在明确收益的时候才使用（数据库内容修改次数较少）。</p>
</blockquote>
<p>确实是这样的！实际项目中，更建议使用本地缓存（比如Caffeine）或者分布式缓存（比如Redis），性能更好，更通用一些</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-query-cache.html">原文链接</a></p>
</blockquote>
<h2 id="Explain执行计划"><a href="#Explain执行计划" class="headerlink" title="Explain执行计划"></a>Explain执行计划</h2><h3 id="什么是执行计划？"><a href="#什么是执行计划？" class="headerlink" title="什么是执行计划？"></a>什么是执行计划？</h3><p><strong>执行计划</strong>是指一条SQL语句在经过<strong>MySQL查询优化器</strong>的优化会后，具体的执行方式。执行计划通常用于SQL性能分析、优化等场景。通过EXPLAIN的结果，可以了解到如数据表的查询顺序、数据查询操作的操作类型、哪些索引可以被命中、哪些索引实际会命中、每个数据表有多少行记录被查询等信息。</p>
<h3 id="如何获取执行计划？"><a href="#如何获取执行计划？" class="headerlink" title="如何获取执行计划？"></a>如何获取执行计划？</h3><p>MySQL为我们提供了EXPLAIN命令，来获取执行计划的相关信息。需要注意的是，EXPLAIN语句并不会真的去执行相关的语句，而是通过查询优化器对语句进行分析，找出最优的查询方案，并显示对应的信息。EXPLAIN执行计划支持SELECT、DELETE、INSERT、REPLACE以及UPDATE语句。我们一般多用于分析SELECT查询语句，使用起来非常简单，语法如下：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">EXPLAIN</span> <span class="token operator">+</span> <span class="token keyword">SELECT</span> 查询语句；<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>我们简单来看下一条查询语句的执行计划：</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">mysql<span class="token operator">></span> <span class="token keyword">explain</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> dept_emp <span class="token keyword">WHERE</span> emp_no <span class="token operator">IN</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> emp_no <span class="token keyword">FROM</span> dept_emp <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> emp_no <span class="token keyword">HAVING</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>emp_no<span class="token punctuation">)</span><span class="token operator">></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----+-------------+----------+------------+-------+-----------------+---------+---------+------+--------+----------+-------------+</span>
<span class="token operator">|</span> id <span class="token operator">|</span> select_type <span class="token operator">|</span> <span class="token keyword">table</span>    <span class="token operator">|</span> partitions <span class="token operator">|</span> <span class="token keyword">type</span>  <span class="token operator">|</span> possible_keys   <span class="token operator">|</span> <span class="token keyword">key</span>     <span class="token operator">|</span> key_len <span class="token operator">|</span> ref  <span class="token operator">|</span> <span class="token keyword">rows</span>   <span class="token operator">|</span> filtered <span class="token operator">|</span> Extra       <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+-------------+----------+------------+-------+-----------------+---------+---------+------+--------+----------+-------------+</span>
<span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span> <span class="token keyword">PRIMARY</span>     <span class="token operator">|</span> dept_emp <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span> <span class="token keyword">ALL</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span>            <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span> <span class="token number">331143</span> <span class="token operator">|</span>   <span class="token number">100.00</span> <span class="token operator">|</span> <span class="token keyword">Using</span> <span class="token keyword">where</span> <span class="token operator">|</span>
<span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span> SUBQUERY    <span class="token operator">|</span> dept_emp <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span> <span class="token keyword">index</span> <span class="token operator">|</span> <span class="token keyword">PRIMARY</span><span class="token punctuation">,</span>dept_no <span class="token operator">|</span> <span class="token keyword">PRIMARY</span> <span class="token operator">|</span> <span class="token number">16</span>      <span class="token operator">|</span> <span class="token boolean">NULL</span> <span class="token operator">|</span> <span class="token number">331143</span> <span class="token operator">|</span>   <span class="token number">100.00</span> <span class="token operator">|</span> <span class="token keyword">Using</span> <span class="token keyword">index</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----+-------------+----------+------------+-------+-----------------+---------+---------+------+--------+----------+-------------+</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>可以看到，执行计划结果中共有12列，各列代表的含义总结如下表：</p>
<table>
<thead>
<tr>
<th><strong>列名</strong></th>
<th><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td>id</td>
<td>SELECT查询的序列标识符</td>
</tr>
<tr>
<td>select_type</td>
<td>SELECT关键字对应的查询类型</td>
</tr>
<tr>
<td>table</td>
<td>用到的表名</td>
</tr>
<tr>
<td>partitions</td>
<td>匹配的分区，对于未分区的表，值为 NULL</td>
</tr>
<tr>
<td>type</td>
<td>表的访问方法</td>
</tr>
<tr>
<td>possible_keys</td>
<td>可能用到的索引</td>
</tr>
<tr>
<td>key</td>
<td>实际用到的索引</td>
</tr>
<tr>
<td>key_len</td>
<td>所选索引的长度</td>
</tr>
<tr>
<td>ref</td>
<td>当使用索引等值查询时，与索引作比较的列或常量</td>
</tr>
<tr>
<td>rows</td>
<td>预计要读取的行数</td>
</tr>
<tr>
<td>filtered</td>
<td>按表条件过滤后，留存的记录数的百分比</td>
</tr>
<tr>
<td>Extra</td>
<td>附加信息</td>
</tr>
</tbody></table>
<h4 id="如何分析EXPLAIN结果？"><a href="#如何分析EXPLAIN结果？" class="headerlink" title="如何分析EXPLAIN结果？"></a>如何分析EXPLAIN结果？</h4><p>为了分析EXPLAIN语句的执行结果，我们需要搞懂执行计划中的重要字段。</p>
<h4 id="id"><a href="#id" class="headerlink" title="id"></a>id</h4><p>SELECT标识符，是查询中SELECT的序号，用来标识整个查询中SELELCT语句的顺序。</p>
<p>id如果相同，从上往下依次执行。id不同，id值越大，执行优先级越高，如果行引用其他行的并集结果，则该值可以为NULL。</p>
<h4 id="select-type"><a href="#select-type" class="headerlink" title="select_type"></a>select_type</h4><p>查询的类型，主要用于区分普通查询、联合查询、子查询等复杂的查询，常见的值有：</p>
<ul>
<li><strong>SIMPLE</strong>：简单查询，不包含UNION或者子查询。</li>
<li><strong>PRIMARY</strong>：查询中如果包含子查询或其他部分，外层的SELECT将被标记为PRIMARY。</li>
<li><strong>SUBQUERY</strong>：子查询中的第一个SELECT。</li>
<li><strong>UNION</strong>：在UNION语句中，UNION之后出现的SELECT。</li>
<li><strong>DERIVED</strong>：在FROM中出现的子查询将被标记为DERIVED。</li>
<li><strong>UNIONRESULT</strong>：UNION查询的结果。</li>
</ul>
<h4 id="table"><a href="#table" class="headerlink" title="table"></a>table</h4><p>查询用到的表名，每行都有对应的表名，表名除了正常的表之外，也可能是以下列出的值：</p>
<ul>
<li><strong>&lt;unionM,N&gt;</strong>:本行引用了id为M和N的行的UNION结果；</li>
<li><strong>&lt; derivedN&gt;</strong>:本行引用了id为N的表所产生的的派生表结果。派生表有可能产生自FROM语句中的子查询。</li>
<li><strong>&lt; subqueryN&gt;</strong>:本行引用了id为N的表所产生的的物化子查询结果。</li>
</ul>
<h4 id="type（重要）"><a href="#type（重要）" class="headerlink" title="type（重要）"></a>type（重要）</h4><p>查询执行的类型，描述了查询是如何执行的。所有值的顺序从最优到最差排序为：system&gt;const&gt;eq_ref&gt;ref&gt;fulltext&gt;ref_or_null&gt;index_merge&gt;unique_subquery&gt;index_subquery&gt;range&gt;index&gt;ALL</p>
<p>常见的几种类型具体含义如下：</p>
<ul>
<li><strong>system</strong>：如果表使用的引擎对于表行数统计是精确的（如：MyISAM），且表中只有一行记录的情况下，访问方法是system，是const的一种特例。</li>
<li><strong>const</strong>：表中最多只有一行匹配的记录，一次查询就可以找到，常用于使用主键或唯一索引的所有字段作为查询条件。</li>
<li><strong>eq_ref</strong>：当连表查询时，前一张表的行在当前这张表中只有一行与之对应。是除了system与const之外最好的join方式，常用于使用主键或唯一索引的所有字段作为连表条件。</li>
<li><strong>ref</strong>：使用普通索引作为查询条件，查询结果可能找到多个符合条件的行。</li>
<li><strong>index_merge</strong>：当查询条件使用了多个索引时，表示开启了Index Merge优化，此时执行计划中的key列列出了使用到的索引。</li>
<li><strong>range</strong>：对索引列进行范围查询，执行计划中的key列表示哪个索引被使用了。</li>
<li><strong>index</strong>：查询遍历了整棵索引树，与ALL类似，只不过扫描的是索引，而索引一般在内存中，速度更快。</li>
<li><strong>ALL</strong>：全表扫描。</li>
</ul>
<h4 id="possible-keys"><a href="#possible-keys" class="headerlink" title="possible_keys"></a>possible_keys</h4><p>possible_keys列表示MySQL执行查询时可能用到的索引。如果这一列为NULL，则表示没有可能用到的索引；这种情况下，需要检查WHERE语句中所使用的的列，看是否可以通过给这些列中某个或多个添加索引的方法来提高查询性能。</p>
<h4 id="key（重要）"><a href="#key（重要）" class="headerlink" title="key（重要）"></a>key（重要）</h4><p>key列表示MySQL实际使用到的索引。如果为NULL，则表示未用到索引。</p>
<h4 id="key-len"><a href="#key-len" class="headerlink" title="key_len"></a>key_len</h4><p>key_len列表示MySQL实际使用的索引的最大长度；当使用到联合索引时，有可能是多个列的长度和。在满足需求的前提下越短越好。如果key列显示NULL，则key_len列也显示NULL。</p>
<h4 id="rows"><a href="#rows" class="headerlink" title="rows"></a>rows</h4><p>rows列表示根据表统计信息及选用情况，大致估算出找到所需的记录或所需读取的行数，数值越小越好。</p>
<h4 id="Extra（重要）"><a href="#Extra（重要）" class="headerlink" title="Extra（重要）"></a>Extra（重要）</h4><p>这列包含了MySQL解析查询的额外信息，通过这些信息，可以更准确的理解MySQL到底是如何执行查询的。常见的值如下：</p>
<ul>
<li><strong>Using filesort</strong>：在排序时使用了外部的索引排序，没有用到表内索引进行排序。</li>
<li><strong>Using temporary</strong>：MySQL需要创建临时表来存储查询的结果，常见于ORDERBY和GROUPBY。</li>
<li><strong>Using index</strong>：表明查询使用了覆盖索引，不用回表，查询效率非常高。</li>
<li><strong>Using index condition</strong>：表示查询优化器选择使用了索引条件下推这个特性。</li>
<li><strong>Using where</strong>：表明查询使用了WHERE子句进行条件过滤。一般在没有使用到索引的时候会出现。</li>
<li><strong>Using join buffer（Block Nested Loop）</strong>：连表查询的方式，表示当被驱动表的没有使用索引的时候，MySQL会先将驱动表读出来放到joinbuffer中，再遍历被驱动表与驱动表进行查询。</li>
</ul>
<p>这里提醒下，当Extra列包含Using file sort或Using temporary时，MySQL的性能可能会存在问题，需要尽可能避免</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://javaguide.cn/database/mysql/mysql-query-execution-plan.html">原文链接</a></p>
</blockquote>
<h3 id="总结-4"><a href="#总结-4" class="headerlink" title="总结"></a>总结</h3><ol>
<li>id: 是用来顺序标识整个查询中select语句的，在嵌套查询中id越大的语句越先执</li>
<li>select_type:查询的类型</li>
</ol>
<ul>
<li>simple: 简单的SELECT（不使用UNION或子查询）</li>
<li>primary: 最外面的SELECT</li>
<li>union: UNION中的第二个或更高的SELECT语句</li>
<li>dependent union: UNION中的第二个或更高的SELECT语句，取决于外部查询</li>
<li>union result: UNION的结果</li>
<li>subquery: 在子查询中首先选择SELECT</li>
<li>dependent subquery: 子查询中的第一个SELECT，取决于外部查询</li>
<li>derived: 派生表——该临时表是从子查询派生出来的，位于from中的子查询</li>
<li>uncacheable subquery: 无法缓存结果的子查询，必须为外部查询的每一行重新计算</li>
<li>uncacheable union: 在UNION中的第二个或更晚的选择属于不可缓存的子查询</li>
</ul>
<ol start="3">
<li>table:当前行所查询的表</li>
<li>type:访问类型，表示数据库引擎查找表的方式。常见的type类型有：all,index,range,ref,eq_ref,const。</li>
</ol>
<ul>
<li>all :全表扫描，表示sql语句会把表中所有表数据全部读取读取扫描一遍。效率最低，我们应尽量避免</li>
<li>index :全索引扫描,表示sql语句将会把整颗二级索引树全部读取扫描一遍，因为二级索引树的数据量比全表数据量小，所以效率比all高一些。一般查询语句中查询字段为索引字段，且无where子句时，type会为index,类似全表扫描，只是扫描表的时候按照索引次序进行而不是行。主要优点就是避免了排序,但是开销仍然非常大。如在Extra列看到Using index，说明正在使用覆盖索引，只扫描索引的数据，它比按索引次序全表扫描的开销要小很多</li>
<li>range :部分索引扫描，当查询为区间查询，且查询字段为索引字段时，这时会根据where条件对索引进行部分扫描,范围扫描，一个有限制的索引扫描。key列显示使用了哪个索引。当使用&#x3D;、&lt;&gt;、&gt;、&gt;&#x3D;、&lt;、&lt;&#x3D;、IS NULL、&lt;&#x3D;&gt;、BETWEEN或者IN操作符，用常量比较关键字列时,可以使用range</li>
<li>unique_subquery: 在使用in查询的情况下会取代eq_ref</li>
<li>index_merge: 此连接类型表示使用了索引合并优化。在这种情况下，输出行中的key列包含使用的索引列表，key_len包含所用索引的最长key部分列表</li>
<li>ref :出现于where操作符为‘&#x3D;’，且where字段为非唯一索引的单表查询或联表查询。通过普通索引查询匹配的很多行时的类型</li>
<li>ref_or_null: 跟ref类似的效果，不过多一个列不能null的条件</li>
<li>eq_ref :出现于where操作符为‘&#x3D;’，且where字段为唯一索引的联表查询。最多只返回一条符合条件的记录，通过使用在两个表有关联字段的时候</li>
<li>const :出现于where操作符为‘&#x3D;’，且where字段为唯一索引的单表查询,此时最多只会匹配到一行,当确定最多只会有一行匹配的时候，MySQL优化器会在查询前读取它而且只读取一次，因此非常快。使用主键查询往往就是const级别的，非常高效</li>
<li>system:const的一种特例，表中只有一行数据<br>fulltext: 全文索引<br><strong>单从type字段考虑效率，const &gt; eq_ref &gt; ref &gt; range &gt; index &gt; all</strong></li>
</ul>
<ol start="5">
<li>possible_keys:查询可能用到的索引,MySQL 可能采用的索引，但是并不一定使用</li>
<li>key:mysql决定采用的索引来优化查询,真正使用的索引名称<br>sql语句实际执行时使用的索引列，有时候mysql可能会选择优化效果不是最好的索引，这时，我们可以在select语句中使用force index(INDEXNAME)来强制mysql使用指定索引或使用ignore index(INDEXNAME)强制mysql忽略指定索引</li>
<li>key_len: 索引key的长度</li>
<li>ref: 显示了之前的表在key列记录的索引中查找值所用的列或常量</li>
<li>rows:查询扫描的行数，预估值，不一定准确</li>
<li>Extra:额外的查询辅助信息</li>
</ol>
<ul>
<li>extra列会包含一些十分重要的信息，我们可以根据这些信息进行sql优化</li>
<li>using index: sql语句没有where查询条件，使用覆盖索引，不需要回表查询即可拿到结果</li>
<li>using where: 没有使用索引&#x2F;使用了索引但需要回表查询且没有使用到下推索引</li>
<li>using index &amp;&amp; useing where: sql语句有where查询条件，且使用覆盖索引，不需要回表查询即可拿到结果。</li>
<li>Using index condition:使用索引查询，sql语句的where子句查询条件字段均为同一索引字段，且开启索引下推功能，需要回表查询即可拿到结果</li>
<li>Using index condition &amp;&amp; using where:使用索引查询，sql语句的where子句查询条件字段存在非同一索引字段，且开启索引下推功能，需要回表查询即可拿到结果。</li>
<li>using filesort: 当语句中存在order by时，且orderby字段不是索引，这个时候mysql无法利用索引进行排序，只能用排序算法重新进行排序，会额外消耗资源。</li>
<li>Using temporary：建立了临时表来保存中间结果，查询完成之后又要把临时表删除。会很影响性能，需尽快优化</li>
</ul>
<ol start="11">
<li>filtered ：查询的表行占表的百分比</li>
<li>partitions：匹配的分区</li>
</ol>
<h3 id="相关文章-4"><a href="#相关文章-4" class="headerlink" title="相关文章"></a>相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/aTkGwVU5D9u1RAbMHQconw">explain都不懂，还好意思说会SQL调优?</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/NwRVW6Z8AMZ_QClTXH71ow">MySQL之Explain输出分析</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/E8wJQvldwEAzxK5mEuFhog">什么是MySQL的执行计划（Explain关键字）？</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/kYcrHtE82-sOqNOp_qM4Ig">MySQL究竟是怎么执行的(explain)</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/-YrZFxTbutLdEbkfa9aOKQ">EXPLAIN进行索引分析和优化</a></td>
<td align="center"></td>
</tr>
</tbody></table>
<h2 id="MySQL运维"><a href="#MySQL运维" class="headerlink" title="MySQL运维"></a>MySQL运维</h2><h3 id="获取基本配置信息"><a href="#获取基本配置信息" class="headerlink" title="获取基本配置信息"></a>获取基本配置信息</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">是否区分表名大小写</td>
<td align="center">SHOW VARIABLES LIKE ‘lower_case_table_names’;</td>
</tr>
<tr>
<td align="center">MySQL服务端口</td>
<td align="center">SHOW VARIABLES LIKE ‘port’;</td>
</tr>
<tr>
<td align="center">MySQL socket文件路径</td>
<td align="center">SHOW VARIABLES LIKE ‘socket’;</td>
</tr>
<tr>
<td align="center">是否跳过域名解析</td>
<td align="center">SHOW VARIABLES LIKE ‘skip_name_resolve’;</td>
</tr>
<tr>
<td align="center">数据库字符集</td>
<td align="center">SHOW VARIABLES LIKE ‘character_set_server’;</td>
</tr>
<tr>
<td align="center">服务器默认的校对规则</td>
<td align="center">SHOW VARIABLES LIKE ‘collation_server’;</td>
</tr>
<tr>
<td align="center">交互式连接超时时间(秒)</td>
<td align="center">SHOW VARIABLES LIKE ‘interactive_timeout’;</td>
</tr>
<tr>
<td align="center">非交互式连接超时时间(秒)</td>
<td align="center">SHOW VARIABLES LIKE ‘wait_timeout’;</td>
</tr>
<tr>
<td align="center">事务隔离级别</td>
<td align="center">SHOW VARIABLES LIKE ‘tx_isolation’;</td>
</tr>
<tr>
<td align="center">数据文件存放位置</td>
<td align="center">SHOW VARIABLES LIKE ‘%datadir%’;</td>
</tr>
<tr>
<td align="center">允许的最大数据包大小</td>
<td align="center">SHOW VARIABLES LIKE ‘max_allowed_packet’;</td>
</tr>
<tr>
<td align="center">每个线程堆栈的大小</td>
<td align="center">SHOW VARIABLES LIKE ‘thread_stack’;</td>
</tr>
<tr>
<td align="center">线程缓存的大小</td>
<td align="center">SHOW VARIABLES LIKE ‘thread_cache_size’;</td>
</tr>
<tr>
<td align="center">可以打开的表的数量</td>
<td align="center">SHOW VARIABLES LIKE ‘table_open_cache’;</td>
</tr>
<tr>
<td align="center">内部临时表的最大大小</td>
<td align="center">SHOW VARIABLES LIKE ‘tmp_table_size’;</td>
</tr>
<tr>
<td align="center">内存中HEAP表的最大大小</td>
<td align="center">SHOW VARIABLES LIKE ‘max_heap_table_size’;</td>
</tr>
<tr>
<td align="center">MySQL主进程文件的位置</td>
<td align="center">SHOW VARIABLES LIKE ‘pid_file’;</td>
</tr>
<tr>
<td align="center">错误日志文件的位置</td>
<td align="center">SHOW VARIABLES LIKE ‘log_error’;</td>
</tr>
<tr>
<td align="center">慢查询日志文件的位置</td>
<td align="center">SHOW VARIABLES LIKE ‘slow_query_log_file’;</td>
</tr>
<tr>
<td align="center">慢查询时间阈值</td>
<td align="center">SHOW VARIABLES LIKE ‘long_query_time’;</td>
</tr>
</tbody></table>
<h3 id="获取连接数配置信息"><a href="#获取连接数配置信息" class="headerlink" title="获取连接数配置信息"></a>获取连接数配置信息</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">最大连接数</td>
<td align="center">SHOW VARIABLES LIKE ‘max_connections’;</td>
</tr>
<tr>
<td align="center">服务器启动以来已使用的最大连接数</td>
<td align="center">SHOW status LIKE ‘Max_used_connections’;</td>
</tr>
<tr>
<td align="center">达到最大连接数的时间点</td>
<td align="center">SHOW STATUS LIKE ‘Max_used_connections_time’;</td>
</tr>
<tr>
<td align="center">连接失败次数上限</td>
<td align="center">SHOW VARIABLES LIKE ‘max_connect_errors’;</td>
</tr>
<tr>
<td align="center">通过MySQLXProtocol已接受的连接数</td>
<td align="center">SHOW STATUS LIKE ‘Mysqlx_connections_accepted’;</td>
</tr>
<tr>
<td align="center">通过MySQLXProtocol已关闭的连接数</td>
<td align="center">SHOW STATUS LIKE ‘Mysqlx_connections_closed’;</td>
</tr>
</tbody></table>
<h3 id="获取Binlog配置信息"><a href="#获取Binlog配置信息" class="headerlink" title="获取Binlog配置信息"></a>获取Binlog配置信息</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">Binlog文件列表</td>
<td align="center">SHOW BINARY LOGS;</td>
</tr>
<tr>
<td align="center">是否启用Binlog</td>
<td align="center">SHOW VARIABLES LIKE ‘log_bin’;</td>
</tr>
<tr>
<td align="center">Binlog同步频率</td>
<td align="center">SHOW VARIABLES LIKE ‘sync_binlog’;</td>
</tr>
<tr>
<td align="center">Binlog格式</td>
<td align="center">SHOW VARIABLES LIKE ‘binlog_format’;</td>
</tr>
<tr>
<td align="center">文件路径</td>
<td align="center">SHOW VARIABLES LIKE ‘log-bin’;</td>
</tr>
<tr>
<td align="center">Binlog索引文件位置</td>
<td align="center">SHOW VARIABLES LIKE ‘log_bin_index’;</td>
</tr>
<tr>
<td align="center">单个Binlog最大大小</td>
<td align="center">SHOW VARIABLES LIKE ‘max_binlog_size’;</td>
</tr>
<tr>
<td align="center">Binlog基本名字</td>
<td align="center">SHOW VARIABLES LIKE ‘log_bin_basename’;</td>
</tr>
<tr>
<td align="center">Binlog文件过期时间</td>
<td align="center">SHOW VARIABLES LIKE ‘expire_logs_days’;</td>
</tr>
<tr>
<td align="center">MySQL服务器的唯一ID</td>
<td align="center">SHOW VARIABLES LIKE ‘server_id’;</td>
</tr>
</tbody></table>
<h3 id="获取GTID配置信息"><a href="#获取GTID配置信息" class="headerlink" title="获取GTID配置信息"></a>获取GTID配置信息</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">是否开启GTID模式</td>
<td align="center">SHOW VARIABLES LIKE ‘gtid_mode’;</td>
</tr>
<tr>
<td align="center">是否强制GTID一致性</td>
<td align="center">SHOW VARIABLES LIKE ‘enforce_gtid_consistency’;</td>
</tr>
<tr>
<td align="center">查询GTID执行的详细情况</td>
<td align="center">SELECT * FROM mysql.gtid_executed;</td>
</tr>
<tr>
<td align="center">是否开启级联复制</td>
<td align="center">SHOW VARIABLES LIKE ‘log_slave_updates’;</td>
</tr>
</tbody></table>
<h3 id="InnoDB配置信息"><a href="#InnoDB配置信息" class="headerlink" title="InnoDB配置信息"></a>InnoDB配置信息</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">InnoDB版本</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_version’;</td>
</tr>
<tr>
<td align="center">是否为每个InnoDB表创建单独的文件</td>
<td align="center">SHOW VARTABLES LIKE ‘innodb_file_per_table’;</td>
</tr>
<tr>
<td align="center">InnoDB缓存池大小</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_buffer_pool_size’;</td>
</tr>
<tr>
<td align="center">InnoDB日志文件大小</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_log_file_size’;</td>
</tr>
<tr>
<td align="center">InnoDB可以有多少线程同时运行</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_thread_concurrency’;</td>
</tr>
<tr>
<td align="center">缓冲池中脏页的最大百分比</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_max_dirty_pages_pct’;</td>
</tr>
<tr>
<td align="center">InnoDB的最大写入IO能力</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_io_capacity_max’;</td>
</tr>
<tr>
<td align="center">日志缓冲区大小</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_log_buffer_size’;</td>
</tr>
<tr>
<td align="center">事务提交时刷新日志的频率</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_flush_log_at_trx_commit’;</td>
</tr>
<tr>
<td align="center">InnoDB IO容量</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_io_capacity’;</td>
</tr>
<tr>
<td align="center">缓冲池实例数</td>
<td align="center">SHOW VARIABLES LIKE ‘innodb_buffer_pool_instances’;</td>
</tr>
</tbody></table>
<h3 id="监控指标"><a href="#监控指标" class="headerlink" title="监控指标"></a>监控指标</h3><table>
<thead>
<tr>
<th align="center">说明</th>
<th align="center">执行语句</th>
</tr>
</thead>
<tbody><tr>
<td align="center">全局状态信息</td>
<td align="center">SHOW GLOBAL STATUS;</td>
</tr>
<tr>
<td align="center">当前所有线程的信息</td>
<td align="center">SHOW PROCESSLIST;</td>
</tr>
<tr>
<td align="center">当前打开的连接数</td>
<td align="center">SHOW STATUS LIKE ‘Threads_connected’;</td>
</tr>
<tr>
<td align="center">当前线程缓存中有多少空闲线程</td>
<td align="center">SHOW STATUS LIKE ‘Threads_cached’;</td>
</tr>
<tr>
<td align="center">从最近一次服务启动，已创建线程的数量</td>
<td align="center">SHOW STATUS LIKE ‘Threads_created’;</td>
</tr>
<tr>
<td align="center">当前激活的(非睡眠状态)线程数</td>
<td align="center">SHOW STATUS LIKE ‘Threads_running’;</td>
</tr>
<tr>
<td align="center">慢查询次数</td>
<td align="center">SHOW status LIKE ‘Slow_queries’;</td>
</tr>
<tr>
<td align="center">InnoDB读取的行数</td>
<td align="center">SHOW status LIKE ‘Innodb_rows_read’;</td>
</tr>
<tr>
<td align="center">InnoDB插入的行数</td>
<td align="center">SHOW status LIKE ‘Innodb_rows_inserted’;</td>
</tr>
<tr>
<td align="center">InnoDB更新的行数</td>
<td align="center">SHOW status LIKE ‘Innodb_rows_updated’;</td>
</tr>
<tr>
<td align="center">InnoDB删除的行数</td>
<td align="center">SHOW status LIKE ‘Innodb_rows_deleted’;</td>
</tr>
</tbody></table>
<h3 id="关键字、常用操作"><a href="#关键字、常用操作" class="headerlink" title="关键字、常用操作"></a>关键字、常用操作</h3><h4 id="exists"><a href="#exists" class="headerlink" title="exists"></a>exists</h4><p>表A（小表），表B（大表）</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> A <span class="token keyword">where</span> c <span class="token operator">in</span> <span class="token punctuation">(</span><span class="token keyword">select</span> c <span class="token keyword">from</span> B<span class="token punctuation">)</span> <span class="token comment">-- ，用到了A表上c列的索引；</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> A <span class="token keyword">where</span> <span class="token keyword">exists</span><span class="token punctuation">(</span><span class="token keyword">select</span> c <span class="token keyword">from</span> B <span class="token keyword">where</span> c<span class="token operator">=</span>A<span class="token punctuation">.</span>c<span class="token punctuation">)</span> <span class="token comment">-- 效率高，用到了B表上c列的索引。</span>
<span class="token comment">-- 相反的</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> B <span class="token keyword">where</span> c <span class="token operator">in</span> <span class="token punctuation">(</span><span class="token keyword">select</span> c <span class="token keyword">from</span> A<span class="token punctuation">)</span> <span class="token comment">-- 效率高，用到了B表上c列的索引</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> B <span class="token keyword">where</span> <span class="token keyword">exists</span><span class="token punctuation">(</span><span class="token keyword">select</span> c <span class="token keyword">from</span> A <span class="token keyword">where</span> c<span class="token operator">=</span>B<span class="token punctuation">.</span>c<span class="token punctuation">)</span> <span class="token comment">-- 效率低，用到了A表上c列的索引。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/O121Y0sywAtt4jk9vy4b0w">MySQL多表联合查询有何讲究？</a></p>
</blockquote>
<h4 id="逃离符escape"><a href="#逃离符escape" class="headerlink" title="逃离符escape"></a>逃离符escape</h4><p>如果要查%或者_，怎么办呢？使用escape，转义字符后面的%或_就不作为通配符了，注意前面没有转义字符的%和_仍然起通配符作用</p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> username <span class="token keyword">from</span> <span class="token keyword">user</span> <span class="token keyword">where</span> username <span class="token operator">like</span> <span class="token string">'%xiao/_%'</span> <span class="token keyword">escape</span> <span class="token string">'/'</span><span class="token punctuation">;</span>
<span class="token keyword">select</span> username <span class="token keyword">from</span> <span class="token keyword">user</span> <span class="token keyword">where</span> username <span class="token operator">like</span> <span class="token string">'%xiao/%%'</span> <span class="token keyword">escape</span> <span class="token string">'/'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h4 id="分组排序"><a href="#分组排序" class="headerlink" title="分组排序"></a>分组排序</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> a<span class="token punctuation">.</span>id<span class="token punctuation">,</span>a<span class="token punctuation">.</span>class<span class="token punctuation">,</span>a<span class="token punctuation">.</span>source
<span class="token keyword">from</span> student a <span class="token keyword">left</span> <span class="token keyword">join</span> student b <span class="token keyword">on</span> a<span class="token punctuation">.</span>class<span class="token operator">=</span>b<span class="token punctuation">.</span>class <span class="token operator">and</span> a<span class="token punctuation">.</span>source<span class="token operator">&lt;=</span>b<span class="token punctuation">.</span>source
<span class="token keyword">group</span> <span class="token keyword">by</span> a<span class="token punctuation">.</span>class<span class="token punctuation">,</span>a<span class="token punctuation">.</span>source
<span class="token keyword">order</span> <span class="token keyword">by</span> a<span class="token punctuation">.</span>class<span class="token punctuation">,</span>a<span class="token punctuation">.</span>source<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/igGKepCsf-SUKChco4P6Lw">阿里二面：group by怎么优化？</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/zc7_PZxwaQAwwqNsRG2_KQ">MySQL中的distinct和group by哪个效率更高？</a></p>
</blockquote>
<h4 id="有就更新-没有就插入"><a href="#有就更新-没有就插入" class="headerlink" title="有就更新,没有就插入"></a>有就更新,没有就插入</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token keyword">table</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> gender<span class="token punctuation">)</span>  <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'Jerry'</span><span class="token punctuation">,</span> <span class="token string">'boy'</span><span class="token punctuation">)</span>  <span class="token keyword">ON</span> <span class="token keyword">DUPLICATE</span> <span class="token keyword">KEY</span> <span class="token keyword">UPDATE</span> name<span class="token operator">=</span><span class="token string">'Jerry'</span>，gender<span class="token operator">=</span><span class="token string">'girl'</span><span class="token punctuation">;</span>
<span class="token comment">-- 或</span>
<span class="token keyword">REPLACE</span> <span class="token keyword">INTO</span> <span class="token keyword">table</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> gender<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'Jerry'</span><span class="token punctuation">,</span> <span class="token string">'girl'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">-- 或</span>
<span class="token keyword">REPLACE</span> <span class="token keyword">INTO</span> <span class="token keyword">table</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> gender<span class="token punctuation">)</span> <span class="token keyword">SELECT</span> <span class="token string">'Jerry'</span><span class="token punctuation">,</span> <span class="token string">'girl'</span><span class="token punctuation">;</span>
<span class="token comment">-- 或</span>
<span class="token keyword">REPLACE</span> <span class="token keyword">INTO</span> test <span class="token keyword">SET</span> name<span class="token operator">=</span><span class="token string">'Jerry'</span><span class="token punctuation">,</span> gender<span class="token operator">=</span><span class="token string">'girl'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="复制表"><a href="#复制表" class="headerlink" title="复制表"></a>复制表</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 不保留数据</span>
<span class="token keyword">create</span> <span class="token keyword">table</span> 表名 <span class="token keyword">as</span> <span class="token keyword">select</span> 列名 <span class="token keyword">from</span> 表名 <span class="token keyword">where</span> <span class="token number">1</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
<span class="token keyword">create</span> <span class="token keyword">table</span> 表名<span class="token punctuation">(</span>列名<span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">select</span> 列名 <span class="token keyword">from</span> 表名 <span class="token keyword">where</span> <span class="token number">1</span><span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
<span class="token comment">-- 保留数据</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> 表名 <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">select</span> a<span class="token punctuation">.</span><span class="token operator">*</span> <span class="token keyword">from</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>a<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="重复数据"><a href="#重复数据" class="headerlink" title="重复数据"></a>重复数据</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 显示重复</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tablename <span class="token keyword">group</span> <span class="token keyword">by</span> id <span class="token keyword">having</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token number">1</span>
<span class="token comment">-- 不显示重复</span>
<span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> tablename <span class="token keyword">group</span> <span class="token keyword">by</span> id <span class="token keyword">having</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/_hgA89R6GWwJgxC9KBb63A">避免MySQL插入重复数据的4种方式</a></p>
</blockquote>
<h4 id="分页"><a href="#分页" class="headerlink" title="分页"></a>分页</h4><p>limit(start,size) start:从第几条记录开始 size:读取几条记录</p>
<h4 id="定时备份"><a href="#定时备份" class="headerlink" title="定时备份"></a>定时备份</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment">#db用户名</span>
<span class="token assign-left variable">dbuser</span><span class="token operator">=</span>root
<span class="token comment">#db密码</span>
<span class="token assign-left variable">dbpasswd</span><span class="token operator">=</span><span class="token string">"123456"</span>
<span class="token comment">#ip地址</span>
<span class="token assign-left variable">dbip</span><span class="token operator">=</span><span class="token number">127.0</span>.0.1
<span class="token comment">#备份的数据库名字前缀</span>
<span class="token assign-left variable">pre_name</span><span class="token operator">=</span><span class="token string">"test"</span>

<span class="token comment">#备份操作的日志文件</span>
<span class="token assign-left variable">bakfile</span><span class="token operator">=</span>/data/sqlbak/log.txt
<span class="token comment">#备份数据的目录</span>
<span class="token assign-left variable">bakdatadir</span><span class="token operator">=</span>/data/sqlbak/bakdata
<span class="token comment">#备份数据的目录-持久化备份</span>
<span class="token assign-left variable">persist_path</span><span class="token operator">=</span>/data/sqlbak/persistdata

<span class="token comment">#备份保存时间,单位:天</span>
<span class="token assign-left variable">dateoutday</span><span class="token operator">=</span><span class="token number">15</span>

<span class="token comment">#备份单个数据库</span>
<span class="token function-name function">bak_single</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
  <span class="token comment">#db name</span>
  <span class="token assign-left variable">sdb</span><span class="token operator">=</span><span class="token variable">$1</span>
  <span class="token comment">#日期字符串</span>
  <span class="token assign-left variable">datestr</span><span class="token operator">=</span><span class="token variable">$2</span>

  <span class="token builtin class-name">echo</span> <span class="token string">"bak_single <span class="token variable">$&#123;sdb&#125;</span>_<span class="token variable">$&#123;datestr&#125;</span>.sql.gz BEGIN..."</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
  <span class="token comment">#备份</span>
  mysqldump -u<span class="token variable">$&#123;dbuser&#125;</span> <span class="token parameter variable">-h</span> <span class="token variable">$&#123;dbip&#125;</span> -p<span class="token variable">$&#123;dbpasswd&#125;</span> --single-transaction  --no-create-db <span class="token parameter variable">-R</span> <span class="token parameter variable">-C</span> <span class="token parameter variable">-B</span> <span class="token variable">$&#123;sdb&#125;</span> <span class="token operator">></span> <span class="token variable">$&#123;sdb&#125;</span>_<span class="token variable">$&#123;datestr&#125;</span>.sql
  <span class="token comment">#注释掉sql脚本中 USE DATABASE 语句</span>
  <span class="token function">sed</span> <span class="token parameter variable">-i</span> <span class="token string">"s/USE/-- USE/"</span> <span class="token variable">$&#123;sdb&#125;</span>_<span class="token variable">$&#123;datestr&#125;</span>.sql
  <span class="token comment">#压缩</span>
  <span class="token function">gzip</span> <span class="token variable">$&#123;sdb&#125;</span>_<span class="token variable">$&#123;datestr&#125;</span>.sql
  <span class="token comment">#记录日志</span>
  <span class="token builtin class-name">echo</span> <span class="token string">"bak_single <span class="token variable">$&#123;sdb&#125;</span>_<span class="token variable">$&#123;datestr&#125;</span>.sql.gz  COMPLETE..."</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#备份所有数据库</span>
<span class="token function-name function">bak_all</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
  <span class="token comment">#日期字符串</span>
  <span class="token assign-left variable">all_datestr</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">date</span> <span class="token string">"+%Y_%m_%d_%H_%M_%S"</span><span class="token variable">)</span></span>

  <span class="token assign-left variable">date_dir</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">date</span> <span class="token string">"+%Y%m%d"</span><span class="token variable">)</span></span>

  <span class="token builtin class-name">echo</span> <span class="token string">"bak_all begin <span class="token variable">$&#123;all_datestr&#125;</span> ===================="</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
  <span class="token comment">#所有的数据库</span>
  <span class="token assign-left variable">alldb</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span>mysql -u$<span class="token punctuation">&#123;</span>dbuser<span class="token punctuation">&#125;</span> <span class="token parameter variable">-h</span> $<span class="token punctuation">&#123;</span>dbip<span class="token punctuation">&#125;</span> -p$<span class="token punctuation">&#123;</span>dbpasswd<span class="token punctuation">&#125;</span> <span class="token parameter variable">-e</span> <span class="token string">"show databases"</span><span class="token variable">`</span></span>
  <span class="token keyword">for</span> <span class="token for-or-select variable">dbname</span> <span class="token keyword">in</span> <span class="token variable">$&#123;alldb&#125;</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
    <span class="token punctuation">&#123;</span>
    <span class="token comment">#只备份指定前缀的数据库</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$&#123;dbname&#125;</span> <span class="token operator">=~</span> <span class="token variable">$&#123;pre_name&#125;</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      bak_single <span class="token variable">$&#123;dbname&#125;</span> <span class="token variable">$&#123;all_datestr&#125;</span> 
      <span class="token keyword">fi</span>
    <span class="token punctuation">&#125;</span><span class="token operator">&amp;</span>
    <span class="token keyword">done</span>
           
    <span class="token comment">#等待所有数据库备份完成</span>
    <span class="token function">wait</span>
    <span class="token comment">#压缩</span>
    <span class="token function">tar</span> czvf dbbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar *_<span class="token variable">$&#123;all_datestr&#125;</span>.sql.gz
    <span class="token comment">#删除</span>
    <span class="token function">rm</span> *_<span class="token variable">$&#123;all_datestr&#125;</span>.sql.gz
    <span class="token comment">#是否存在当天日期命名的目录</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> <span class="token parameter variable">-d</span> <span class="token variable">$&#123;bakdatadir&#125;</span>/<span class="token variable">$&#123;date_dir&#125;</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      <span class="token function">mkdir</span> <span class="token parameter variable">-p</span> <span class="token variable">$&#123;bakdatadir&#125;</span>/<span class="token variable">$&#123;date_dir&#125;</span>
      <span class="token keyword">fi</span>
      <span class="token comment">#移动到当天日期命名的目录中</span>
      <span class="token function">mv</span> dbbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar <span class="token variable">$&#123;bakdatadir&#125;</span>/<span class="token variable">$&#123;date_dir&#125;</span>
      <span class="token builtin class-name">echo</span> <span class="token string">"bak_all finish dbbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar ==============="</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#备份所有数据库-不会定时删除</span>
<span class="token function-name function">bak_all_persist</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
  <span class="token comment">#日期字符串</span>
  <span class="token assign-left variable">all_datestr</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">date</span> <span class="token string">"+%Y_%m_%d_%H_%M_%S"</span><span class="token variable">)</span></span>

  <span class="token builtin class-name">echo</span> <span class="token string">"bak_all_persist begin <span class="token variable">$&#123;all_datestr&#125;</span> ===================="</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
  <span class="token comment">#所有的数据库</span>
  <span class="token assign-left variable">alldb</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span>mysql -u$<span class="token punctuation">&#123;</span>dbuser<span class="token punctuation">&#125;</span> <span class="token parameter variable">-h</span> $<span class="token punctuation">&#123;</span>dbip<span class="token punctuation">&#125;</span> -p$<span class="token punctuation">&#123;</span>dbpasswd<span class="token punctuation">&#125;</span> <span class="token parameter variable">-e</span> <span class="token string">"show databases"</span><span class="token variable">`</span></span>
    <span class="token keyword">for</span> <span class="token for-or-select variable">dbname</span> <span class="token keyword">in</span> <span class="token variable">$&#123;alldb&#125;</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
      <span class="token punctuation">&#123;</span>
        <span class="token comment">#只备份指定前缀的数据库</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$&#123;dbname&#125;</span> <span class="token operator">==</span> <span class="token variable">$&#123;pre_name&#125;</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
          bak_single <span class="token variable">$&#123;dbname&#125;</span> <span class="token variable">$&#123;all_datestr&#125;</span>
          <span class="token keyword">fi</span>
      <span class="token punctuation">&#125;</span><span class="token operator">&amp;</span>
      <span class="token keyword">done</span>
           
      <span class="token comment">#等待所有数据库备份完成</span>
      <span class="token function">wait</span>

      <span class="token comment">#压缩</span>
      <span class="token function">tar</span> czvf dbpersistbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar *_<span class="token variable">$&#123;all_datestr&#125;</span>.sql.gz
      <span class="token comment">#删除</span>
      <span class="token function">rm</span> *_<span class="token variable">$&#123;all_datestr&#125;</span>.sql.gz
      <span class="token comment">#是否存在当天日期命名的目录</span>
      <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> <span class="token parameter variable">-d</span> <span class="token variable">$&#123;persist_path&#125;</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token function">mkdir</span> <span class="token parameter variable">-p</span> <span class="token variable">$&#123;persist_path&#125;</span>
        <span class="token keyword">fi</span>
        <span class="token comment">#移动持久化备份的目录中</span>
        <span class="token function">mv</span> dbpersistbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar <span class="token variable">$&#123;persist_path&#125;</span>
        <span class="token comment">#</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"bak_all_persist finish dbbak_<span class="token variable">$&#123;all_datestr&#125;</span>.tar ===================="</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#检查过期备份</span>
<span class="token function-name function">check_date_out</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">&#123;</span>
  <span class="token comment">#当前目录</span>
  <span class="token assign-left variable">curpath</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">pwd</span><span class="token variable">`</span></span>
  <span class="token comment">#当前日期</span>
  <span class="token assign-left variable">curdate</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">date</span> <span class="token string">"+%Y%m%d"</span><span class="token variable">)</span></span>
  <span class="token comment">#最早的保存日期</span>
  <span class="token assign-left variable">lastdate</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">date</span> <span class="token parameter variable">-d</span> <span class="token string">"<span class="token variable">$&#123;curdate&#125;</span> - <span class="token variable">$&#123;dateoutday&#125;</span> day"</span> +%Y%m%d<span class="token variable">`</span></span>
  <span class="token comment">#进入备份目录</span>
  <span class="token builtin class-name">cd</span> <span class="token variable">$&#123;bakdatadir&#125;</span>
  <span class="token comment">#目录列表</span>
  <span class="token assign-left variable">pathlst</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">ls</span><span class="token variable">`</span></span>
  <span class="token comment">#检查目录是否过期，删除已过期的目录</span>
  <span class="token keyword">for</span> <span class="token for-or-select variable">tmpdate</span> <span class="token keyword">in</span> <span class="token variable">$&#123;pathlst<span class="token punctuation">[</span>*<span class="token punctuation">]</span>&#125;</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$&#123;tmpdate&#125;</span> <span class="token parameter variable">-le</span> <span class="token variable">$&#123;lastdate&#125;</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      <span class="token function">rm</span> <span class="token parameter variable">-rf</span> <span class="token variable">$&#123;tmpdate&#125;</span>
      <span class="token builtin class-name">echo</span> <span class="token string">"check_date_out, curdate:<span class="token variable">$&#123;curdate&#125;</span> delete <span class="token variable">$&#123;tmpdate&#125;</span> "</span> <span class="token operator">>></span> <span class="token variable">$&#123;bakfile&#125;</span>
      <span class="token keyword">fi</span>
      <span class="token keyword">done</span>
      <span class="token comment">#回到当前目录</span>
      <span class="token builtin class-name">cd</span> <span class="token variable">$&#123;curpath&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">case</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token keyword">in</span>
   s<span class="token punctuation">)</span>
      bak_single <span class="token variable">$2</span> <span class="token variable">$3</span>
      <span class="token punctuation">;</span><span class="token punctuation">;</span>
   a<span class="token punctuation">)</span>
      bak_all
      <span class="token punctuation">;</span><span class="token punctuation">;</span>
   p<span class="token punctuation">)</span>
      bak_all_persist
      <span class="token punctuation">;</span><span class="token punctuation">;</span>
  chk<span class="token punctuation">)</span> 
      check_date_out
      <span class="token punctuation">;</span><span class="token punctuation">;</span>
   *<span class="token punctuation">)</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Please use correct command..."</span>
      <span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><strong>函数功能</strong><br>备份是以数据库为单位进行备份的，先备份单个数据库，然后再把所有的备份数据库打包一起</p>
<p>bak_single函数表示备份单个数据，传入参数是需要备份的数据库名字和日期字符串，备份文件名由这两个参数构成，也就是说，备份的文件名由数据库名+日期组成，比如test1_2021_08_16_10_05_30.sql表示test1数据库的备份文件，备份的时间2021_08_16_10_05_30</p>
<p>bak_all函数表示备份所有的数据库，不需要传入参数，先执行SQL语句show databases查询出所有的数据库，然后过滤出我们需要备份的数据库，脚本中需要备份的数据库名都是以test开头的，具体的过滤规则可以按照各自的需求自行修改</p>
<p>bak_all函数for循环体中{和}以及它们后面的&amp;表示启动一个新进程并执行大括号中间的命令，也即每个数据库启动一个进程进行备份，for循环结束之后的wait命令表示等待for循环中所有进程结束，也就是等待所有数据库备份全部完成之后，才会执行wait后面的命令,全部备份完成之后，会创建一个以当前日期命名的目录，并打包所有备份的数据库的SQL脚本，放到此目录中，同时删除原始的备份文件</p>
<p>这里采用的是分库备份，分库备份的好处是：如果所有库都备份成一个备份文件时，恢复其中一个库的数据是比较麻烦的，所以采用分库备份，利于恢复单库数据</p>
<p>check_date_out函数是检查备份目录是否过期，如果过期的话，直接删除过期的目录，脚本开头的变量dateoutday指定了备份保留的天数</p>
<p>bak_all_persist函数是持久备份，备份过程和bak_all函数一样，只不过这里是备份到另一个持久数据的目录中，脚本开头的变量persist_path指定了持久备份目录，目录里面的备份文件不会自动删除，需要手工去删除</p>
<p>持久化目录的主要应用场景：有时线上数据库表有数据校正或者表格结构有变动，为了防止误操作，再执行操作之前，先调用bak_all_persist函数备份下数据库，这样，即使出现误操作，还能恢复数据</p>
<p>备份参数说明<br>–single-transaction<br>此选项会将隔离级别设置为可重复读(REPEATABLE READ),让整个数据在dump过程中保证数据的一致性，且不会锁表，这个选项对导出InnoDB的数据表很有用</p>
<p>–no-create-db<br>正常导出的SQL脚本中会有类似CREATE DATABASE语句，加了–no-create-db选项之后就没有此语句了</p>
<p>-C<br>服务器传给客户端的过程中先压缩再传递</p>
<p>-R<br>导出存储过程以及自定义函数</p>
<p>在mysqldump导出数据库SQL脚本之后，sed -i “s&#x2F;USE&#x2F;– USE&#x2F;“ ${sdb}_${datestr}.sql命令的作用是注释掉SQL脚本中的USE DATABASE XXX语句，这个也比较实用的，有时候线上数据会导入到内网，重现线上的一些BUG，但是内网可能已经有一个同名的数据库了，如果注释了这行语句，就可以导入到其他数据库，否则，需要先手工处理SQL脚本，然后再导入</p>
<p><strong>如何使用</strong><br>假如备份MySQL脚本的名字是bak.sh,下面是脚本的使用方法</p>
<p>备份对单个数据库</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 备份logindb数据库</span>
./bak.sh logindb <span class="token string">"2021_08_16_10_05_30"</span>
<span class="token comment"># 执行上述命令后，会在当前目录下生成名为logindb_2021_08_16_10_05_30.sql.gz的文件</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>备份所有数据库<br><code>./bak.sh a</code><br>检查备份保留时间<br><code>./bak.sh chk</code><br>持久化备份<br><code>./bak.sh p</code><br>添加定时任务<br>要实现自动备份功能，还需要添加定时任务，间隔指定时间调用备份脚本，执行ctrontab -e命令，输入以下语句</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">*/10 * * * * /data/sqlbak/bak.sh a
*/15 * * * * /data/sqlbak/bak.sh chk<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>上述定时任务是每10分钟备份一次所有数据库，每15分钟检查一次过期的备份，当然，具体的备份策略根据具体的场景不同，可以根据实际情况调整</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/427491577">一个自动备份MySQL的脚本</a></p>
</blockquote>
<h4 id="查看数据大小"><a href="#查看数据大小" class="headerlink" title="查看数据大小"></a>查看数据大小</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 进入information_schema数据库（存放了其他的数据库的信息）</span>
<span class="token keyword">use</span> information_schema<span class="token punctuation">;</span>

mysql<span class="token operator">></span> <span class="token keyword">use</span> information_schema
Reading <span class="token keyword">table</span> information <span class="token keyword">for</span> completion <span class="token keyword">of</span> <span class="token keyword">table</span> <span class="token operator">and</span> <span class="token keyword">column</span> names
You can turn <span class="token keyword">off</span> this feature <span class="token keyword">to</span> get a quicker startup <span class="token keyword">with</span> <span class="token operator">-</span>A

<span class="token comment">-- 查询所有数据库数据大小</span>
<span class="token keyword">select</span> concat<span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>data_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'MB'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">data</span> <span class="token keyword">from</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span><span class="token punctuation">;</span>

<span class="token comment">-- 查看指定数据库数据的大小</span>
<span class="token keyword">select</span> concat<span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>data_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'MB'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">data</span> <span class="token keyword">from</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">where</span> table_schema<span class="token operator">=</span><span class="token string">'database_name'</span><span class="token punctuation">;</span>

<span class="token comment">-- 查看所有数据库的大小,以MB为单位</span>
<span class="token keyword">SELECT</span> table_schema <span class="token keyword">AS</span> <span class="token string">"Database"</span><span class="token punctuation">,</span><span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token function">SUM</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token string">"Size (MB)"</span>
<span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> table_schema<span class="token punctuation">;</span>

<span class="token comment">-- 查看指定数据库的某个表的数据大小</span>
<span class="token keyword">select</span> concat<span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>data_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'MB'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">data</span> <span class="token keyword">from</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">where</span> table_schema<span class="token operator">=</span><span class="token string">'database_name'</span> <span class="token operator">and</span> table_name<span class="token operator">=</span><span class="token string">'table_name'</span><span class="token punctuation">;</span>

<span class="token comment">-- 查询索引+数据大小</span>
<span class="token keyword">select</span> concat<span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>data_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'MB'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token string">'数据大小'</span> <span class="token punctuation">,</span> concat<span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>index_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'MB'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token string">'索引大小'</span><span class="token punctuation">,</span> <span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>data_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span>index_length<span class="token operator">/</span><span class="token number">1024</span><span class="token operator">/</span><span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token string">'all'</span> <span class="token keyword">from</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">where</span> table_schema<span class="token operator">=</span><span class="token string">'数据库名称'</span><span class="token punctuation">;</span>

<span class="token comment">-- 查看数据表的大小并根据大小排序</span>
<span class="token comment">-- 查询所有表的空间使用情况</span>
<span class="token keyword">SELECT</span> 
    table_schema <span class="token keyword">AS</span> <span class="token identifier"><span class="token punctuation">`</span>Database<span class="token punctuation">`</span></span><span class="token punctuation">,</span>
    table_name <span class="token keyword">AS</span> <span class="token identifier"><span class="token punctuation">`</span>Table<span class="token punctuation">`</span></span><span class="token punctuation">,</span>
    <span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token identifier"><span class="token punctuation">`</span>Size (MB)<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> 
    information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span>
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> 
    <span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token keyword">DESC</span>
<span class="token comment">-- 针对特定数据库查询</span>
<span class="token keyword">SELECT</span> 
    table_name <span class="token keyword">AS</span> <span class="token identifier"><span class="token punctuation">`</span>Table<span class="token punctuation">`</span></span><span class="token punctuation">,</span>
    <span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token identifier"><span class="token punctuation">`</span>Size (MB)<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> 
    information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span>
<span class="token keyword">WHERE</span> 
    table_schema <span class="token operator">=</span> <span class="token string">'your_database_name'</span>
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> 
    <span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token keyword">DESC</span>
<span class="token comment">-- 或者`SHOW TABLE STATUS FROM your_database_name;`</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="分析CPU使用率过高"><a href="#分析CPU使用率过高" class="headerlink" title="分析CPU使用率过高"></a>分析CPU使用率过高</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 1. 使用SHOW VARIABLES LIKE 'performance_schema'; 确保performance_schema是开启的。这个模式可以为你提供关于服务器内部操作的详细信息，这对于诊断性能问题非常有用。</span>
<span class="token comment">-- 2. 查看数据库连接数</span>
<span class="token keyword">show</span> <span class="token keyword">status</span> <span class="token operator">like</span> <span class="token string">'%threads_connected%'</span><span class="token punctuation">;</span>
<span class="token comment">-- 3. 查看进程,高CPU使用的查询可能会在这里显示。</span>
<span class="token keyword">SHOW</span> PROCESSLIST<span class="token punctuation">;</span> <span class="token comment">-- 或SHOW FULL PROCESSLIST</span>
<span class="token comment">-- 4. EXPLAIN执行计划</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>使用慢查询日志。请注意，慢查询日志可能会对数据库性能产生一定的影响，因为它需要记录和写入日志文件。因此，在生产环境中使用慢查询日志时，应该谨慎考虑其影响，并根据需要进行适当的调整。要使用MySQL的慢查询日志，可以按照以下步骤进行操作：</p>
<ol>
<li>首先，确保MySQL的慢查询日志功能已经启用。你可以通过编辑MySQL的配置文件（通常是my.cnf或my.ini）来实现。在[mysqld]部分下添加或修改以下行：</li>
</ol>
<pre class="line-numbers language-none"><code class="language-none"># slow_query_log参数用于启用或禁用慢查询日志功能。1表示启用，0表示禁用
slow_query_log &#x3D; 1
# slow_query_log_file参数用于指定慢查询日志文件的路径
slow_query_log_file &#x3D; &#x2F;path&#x2F;to&#x2F;your&#x2F;logfile.log
# long_query_time参数用于设置查询执行时间的阈值（以秒为单位）
long_query_time &#x3D; 2  # 设置阈值为2秒，你可以根据需要调整这个值<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ol start="2">
<li>重启MySQL服务：修改配置文件后，需要重启MySQL服务以使更改生效。根据你的操作系统和MySQL的安装方式，可以使用相应的命令来重启MySQL服务。</li>
<li>记录慢查询日志：一旦慢查询日志功能启用，MySQL将会记录执行时间超过阈值的查询。这些查询将被写入你在配置文件中指定的日志文件中。你可以使用任何文本编辑器或日志查看工具来查看和查看慢查询日志文件。</li>
<li>分析慢查询日志：一旦你有了慢查询日志文件，可以使用工具如mysqldumpslow来分析日志文件，找出执行缓慢的查询。mysqldumpslow是一个Perl脚本，可以用来分析慢查询日志文件并统计各种查询的执行情况。你可以在终端中运行以下命令来使用mysqldumpslow：</li>
</ol>
<pre class="line-numbers language-perl" data-language="perl"><code class="language-perl">mysqldumpslow <span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>your<span class="token operator">/</span>logfile<span class="token operator">.</span>log<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>这将显示慢查询日志中的统计信息，包括执行次数、总执行时间、平均执行时间等。你可以根据这些信息找出需要优化的查询。</p>
<h4 id="where条件没有数据就用另一个条件"><a href="#where条件没有数据就用另一个条件" class="headerlink" title="where条件没有数据就用另一个条件"></a>where条件没有数据就用另一个条件</h4><p>查询where条件时如果这个没数据就使用另外一个值的数据。比如表t1字段type，当<code>select * from t1 where type = 1</code>没数据时就使用<code>where type=2</code></p>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token punctuation">(</span>
    <span class="token keyword">SELECT</span> <span class="token operator">*</span>
    <span class="token keyword">FROM</span> t1
    <span class="token keyword">WHERE</span> <span class="token keyword">type</span> <span class="token operator">=</span> <span class="token number">1</span>
<span class="token punctuation">)</span>
<span class="token keyword">UNION</span> <span class="token keyword">ALL</span>
<span class="token punctuation">(</span>
    <span class="token keyword">SELECT</span> <span class="token operator">*</span>
    <span class="token keyword">FROM</span> t1
    <span class="token keyword">WHERE</span> <span class="token keyword">type</span> <span class="token operator">=</span> <span class="token number">2</span>
    <span class="token operator">AND</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span> <span class="token punctuation">(</span>
        <span class="token keyword">SELECT</span> <span class="token number">1</span>
        <span class="token keyword">FROM</span> t1
        <span class="token keyword">WHERE</span> <span class="token keyword">type</span> <span class="token operator">=</span> <span class="token number">1</span>
    <span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>首先查询type &#x3D; 1的所有数据。如果type &#x3D; 1没有数据，则查询type &#x3D; 2的所有数据。这种情况下，可以使用UNION和LIMIT结合的方式来实现。具体思路是：先查询type &#x3D; 1的数据。如果type &#x3D; 1没有数据，再查询type &#x3D; 2的数据。使用UNION将两个查询结果合并，并通过LIMIT控制返回的数据。</p>
<ol>
<li>EXISTS的作用：EXISTS用于检查子查询是否返回任何结果。如果子查询返回至少一行数据，EXISTS返回true；否则返回false。</li>
<li>NOT EXISTS的作用：NOT EXISTS是EXISTS的反义。如果子查询没有返回任何数据，NOT EXISTS返回true；否则返回false。</li>
<li>SELECT 1的作用：在EXISTS子查询中，SELECT 1是一种常见的写法。它表示“只要子查询返回任何一行数据即可”，具体的值（如1）并不重要</li>
</ol>
<h4 id="MySQL批量终止所有Waiting-for-handler-commit的线程"><a href="#MySQL批量终止所有Waiting-for-handler-commit的线程" class="headerlink" title="MySQL批量终止所有Waiting for handler commit的线程"></a>MySQL批量终止所有Waiting for handler commit的线程</h4><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> CONCAT<span class="token punctuation">(</span><span class="token string">'KILL '</span><span class="token punctuation">,</span> GROUP_CONCAT<span class="token punctuation">(</span>ID<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">';'</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> kill_command
<span class="token keyword">FROM</span> INFORMATION_SCHEMA<span class="token punctuation">.</span>PROCESSLIST
<span class="token keyword">WHERE</span> STATE <span class="token operator">=</span> <span class="token string">'Waiting for handler commit'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>执行后，会生成一个KILL命令，例如：KILL 5,10,15;然后手动执行生成的命令。</p>
<h2 id="相关文章-5"><a href="#相关文章-5" class="headerlink" title="相关文章"></a>相关文章</h2><h3 id="InnoDB"><a href="#InnoDB" class="headerlink" title="InnoDB"></a>InnoDB</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/FHoOdlWyefQLJ34MkQgFvA">InnoDB自增原理都搞不清楚，还怎么CRUD？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/V0BX5tRlrrZhfe2-ui2aRA">InnoDB原理篇：聊聊数据页变成索引这件事</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Io64o3tbEcf150dS5_ltbQ">两万字详解！InnoDB锁专题！</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/6BoGlaYpdDjzZy19YhInEw">MySQL存储引擎InnoDB详解</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/sr5tQF7lNjTcvwg7Fr6HjQ">innodb是如何存数据的</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ymWeGlaBYWYmfogVDFHo5w">MySQL是如何查询数据的</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/KbOiJ8SKJ_wFZcIyDVGD9g">MySQL是如何实现ACID的?</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/qJINIZ3QcXODHSMm1RTPKQ">一文讲清，MySQL数据库一行数据在磁盘上是怎么存储的？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Oiefzj2b3NZxTwjnRdAFRw">一文讲述MySQL所有的存储引擎</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/s2c9L2p5kGC_InjorgDRnQ">MySQL的InnoDB引擎原来是这样的</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/7Xx7rdl7dabbTNJOOLlFVg">InnoDB事务隔离级别及其实现原理</a></td>
<td align="center"></td>
</tr>
</tbody></table>
<h3 id="主从复制"><a href="#主从复制" class="headerlink" title="主从复制"></a>主从复制</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/OHhX9XCQJnrBYdA27ipHHg">MySQL的主从如何配置</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/xES94DmApf_GGYvT1Ku5QQ">MySQL主从复制</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2JtNBnKShIe-0pgt3nYGkg">面试官：Mysql中主库跑太快，从库追不上怎么整？</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/gUoDR4WLeHQYzyGtmR5K3w">MySQL定时备份的几种方式，这下稳了！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/zOOBkcl8Ns7Kk-k-WaXNhw">MySQL怎么保证备份数据的一致性？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/jgM0aZY7UJSA2NMnkmKO3Q">删库不跑路！我含泪写下了MySQL数据恢复大法…</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/SXnwM_n8UVMkKF04cDVNtw">京东一面：MySQL主备延迟有哪些坑？主备切换策略</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ALcivJKxtTZUHS-HTnSsXA">MySQL主从数据不一致，怎么办？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/bq-sMw6mJg4I_TCvHZ-Zcw">一文讲解MySQL的主从复制</a></td>
</tr>
</tbody></table>
<h3 id="other"><a href="#other" class="headerlink" title="other"></a>other</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/zv25dNB8FXn3KrnN3uJ3uQ">MySQL自增主键为何不是连续的呢？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/c6Dx1xEh2BNIuGpxQahAuw">线上MySQL的自增id用尽怎么办？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/BHE7YTOlo6UZ2WQzRieDxA">MySQL的自增id</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5o-sAOFpmjRSqIoLUNesOQ">MySQL用limit为什么会影响性能？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/1pmVqHBS0CyFJW9ykySF_Q">MySQL查询limit 1000,10和limit 10速度一样快吗？深度分页如何破解</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/HdE5QiK5Qp3_sGd_cXABIg">MySQL体系架构简介</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/HWQbWSQE4O5ndI-NG1Hwwg">炸裂！MySQL82张图带你飞！※</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/9Ex9tzoQCAEUZNU6iXLHgQ">MySQL面试必会！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/FUyRHpPgw0LFWEOPTPmT0g">MySQL都不清楚？直接挂了!</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/_Gd-lBfJnhwczNQA4IJyGg">最全91道MySQL面试题</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/XcNZeHdaMgx35dFoB4_n4A">138张图带你MySQL入门</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ar35RqaoDgasO-OY0TnDpQ">专治MySQL乱码，再也不想看到� �！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ImB0O-Z8IXldvt6YQk80jg">MySQL的varchar水太深了，你真的会用吗？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/vdz0aS8qgHHHhvZWPuaE8A">聊聊MySQL的10大经典错误</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/3gc8tMfnih7WQoPwTtN8IA">MySQ8.0推出直方图，性能大大提升！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ZAzRtRnYGhvie8MDZvy1Gg">MySQL模糊查询再也用不着like+%了！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/PeZ8Py3NNb4CU2BUtxpygg">MySQL最大建议行数2000w,靠谱吗？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/1XmvVNYqt5KycmD8pJanug">图解MVCC！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/WZa5UKYgU-pYKbvovLt1YQ">再有人问你什么是MVCC，就把这篇文章发给他！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/meS5Au1o9qdrSv7505mIcA">MySQL最朴素的监控方式</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/c71ATJLT6_KXtb_iiUlMjg">1亿条数据批量插入MySQL，哪种方式最快</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/LvJCi-8cF6HuLo6UXY0Ing">6种MySQL数据库平滑扩容方案剖析</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/rDdUBw803tvjHkJALY5P2w">什么是插入意向锁？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/XbSADUXIz1aw0kqp7p5urQ">从MySQL读取100w数据进行处理，应该怎么做</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/vyrLOH1NRXXQd0oEnv0M1Q">MySQL误删数据不用跑路了，快速恢复指南来了</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ZCM2FYzKw24Fk8yv4a9b0w">MySQL单表数据最大不要超过多少行？为什么</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/w2wrccywJWM-zXceCR_JXw">Mysql如何做表分区</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wSnniPaj5s1-cJp_VICXzw">大白话讲解Mysql中SQL语句的执行流程</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/iCZA97Vm-lxDKEcR4EJPRQ">盘点Mysql对SQL语句的一些隐式优化技术！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ZwdcTNXKhOEA-T1qUjw-8w">一文带你快速入门Mysql基础知识</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/edzWAy0A_bTzWWtTzI3q3g">主键使用自增还是UUID?(uuid_to_bin(UUID())</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/1YQvVj2Uv7yKjk_nuvwLRQ">突然忘记Mysql登陆密码，怎么办？</a></td>
<td align="center"></td>
</tr>
</tbody></table>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/blog/about" rel="external nofollow noreferrer">xmxe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xmxe.github.io/blog/posts/c12d32c74ecf/">https://xmxe.github.io/blog/posts/c12d32c74ecf/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/blog/about" target="_blank">xmxe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        
                            <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                        
                    </div>
                    <div id="wechat">
                        
                            <img src="/blog/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/posts/e47eee220e74/">
                    <div class="card-image">
                        
                        <img src="https://pic2.zhimg.com/v2-a576f4f763928a2187ec87f161b3f5bc_1440w.jpg" class="responsive-img" alt="Spring相关">
                        
                        <span class="card-title">Spring相关</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Spring注解、循环依赖、Spring MVC、XMl配置、Spring Boot
                        

                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-29
                            -->

                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/Spring/" class="post-category">
                                        Spring
                                    </a>
                                
                            

                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/posts/f5d8fbdfd125/">
                    <div class="card-image">
                        
                        <img src="https://picx1.zhimg.com/v2-ff627d205bd26ab15e4bdefd5daf1dda_1440w.jpg" class="responsive-img" alt="JavaScript相关">
                        
                        <span class="card-title">JavaScript相关</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            NoteJQuery、JS常用方法// json字符串
var str1 = '&#123;"name":"cxh","sex":"man"&#125;';
// json对象
var str2 = &#123;name:"cxh",sex
                        
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-29
                            -->
                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/JS/" class="post-category">
                                        JS
                                    </a>
                                
                            
                                
                                <a href="/blog/tags/%E4%BB%A3%E7%A0%81%E5%AE%9E%E6%88%98/">
                                    <span class="chip bg-color">代码实战</span>
                                </a>
                                
                            
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="569200213"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script> -->

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
            
                <span id="year">2022-2025
                </span>
            
            

            <a href="/blog/about" target="_blank">
                xmxe
            </a>
            |&nbsp;Powered by&nbsp;
            <a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>

            
            
            
            
            
            

            
            <br>

            
            <br>

            
        </div>
        <div class="col s12 m4 l4 social-link2 ">
    <a href="https://github.com/xmxe" class="tooltipped" target="_blank" data-tooltip="GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/xmxe" class="tooltipped" target="_blank" data-tooltip="码云" data-position="top" data-delay="50">
        <svg width="19" height="19" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="position: relative; top: 2px; left: 0.5px;">
            <path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#fff">
            </path>
        </svg>
    </a>

















    
        
          <a href="/blog/download" class="tooltipped" target="_blank" data-tooltip="下载" data-position="top" data-delay="50">
            <i class="fas fa-download"></i>
          </a>
        
    



    <style>
  .mobiledevice {
    display: none !important;
  }

  footer .wechat_qrcode {
    position: fixed;
  }

  /*微信二维码*/
  .wechat_qrcode {
    position: absolute;
    margin-left: 10px;
    bottom: 10px;
    background: url("/blog/medias/xcx.png");
    zoom:40%;
  }

  .wechat:hover .wechat_qrcode {
    width: 430px;
    height: 430px;
    animation: move 0.4s linear 1 normal;
  }

  @keyframes move {
    0% {
      transform: translate(100px, 0);
      opacity: 0;
    }
    50% {
      transform: translate(50px, 0);
      opacity: 0.5;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
    }
  }

  @media only screen and (max-width: 601px) {
    .wechat {
      display: none !important;
    }
    .mobiledevice {
      display: inline-block !important;
    }
  }
</style>

<a href="javascript:;" class="wechat" data-position="top" data-delay="50">
  <i class="fab fa-weixin"></i>
  <img class="wechat_qrcode" />
</a>

<a
  href="javascript:;"
  class="tooltipped mobiledevice"
  data-tooltip="微信: 464817304"
  data-position="top"
  data-delay="50"
>
  <i class="fab fa-weixin"></i>
</a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <div class="stars-con">
  <div id="stars"></div>
  <div id="stars2"></div>
  <div id="stars3"></div>  
</div>

<!-- 白天和黑夜主题 -->



<script>
  function switchNightMode() {
    
      setTimeout(function () {
        $('body').hasClass('DarkMode') 
        ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
        : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
          
        setTimeout(function () {
          $('.Cuteen_DarkSky').fadeOut(1e3, function () {
            $(this).remove()
          })
        }, 2e3)
      })
  }
</script>
    
    
    <script>
        /* 模式判断 */
        if (localStorage.getItem('isDark') === '1') {
            document.body.classList.add('DarkMode');
            $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')
        } else {
            document.body.classList.remove('DarkMode');
            $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')
        }
    </script>

    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    

    

    

	
    

    

    
    <script type="text/javascript" src="/blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    

    <!-- 冒泡 -->
    
    <script type="text/javascript">
        // 只在桌面版网页启用特效
        // var windowWidth = $(window).width();
        
            document.write('<script type="text/javascript" src="/blog/libs/others/bubleAll.js"><\/script>');
        
        
    </script>
    

    <!-- 弹出文字 -->
    

</body>

</html>
